📜 ⬆️ ⬇️

Improving admin panel

One of the weak points of django admin is the main page. The idea of ​​auto-grouping models by application and outputting in one column only works at the initial stages, then it becomes just inconvenient - a lot of unnecessary information and rather complicated ways to add useful information. Well, for example, that the names of applications were written in Russian letters - completely overlap the pattern.

And here django-admin-tools comes to the rescue. With this application, in about 20 minutes you can get a “dashboard” with arbitrary grouping of applications / models, tabs, any number of columns, various blocks that each user can arrange, as he is more comfortable, hide and collapse at will, bookmarks, customizable menus and convenient way to add to all this economy all that you can think of.

So, for example, now the admin to the site of NadoVmeste looks like:
')
image
(this is only a part, here is a screenshot )

We will understand in more detail.

Installation


1. pip install -e hg+http://bitbucket.org/izi/django-admin-tools/#egg=django-admin-tools
We put the dev version, because I'm a fan of dev versions there are a lot of new tasty things: for example, the API is simplified and groups are added. If mercurial is not installed, you can download the latest revision archive , unpack and run python setup.py install .

2. Add admin_tools to INSTALLED_APPS (required before django.contrib.admin):

 INSTALLED_APPS = (
     'admin_tools',
     'admin_tools.theming',
     'admin_tools.menu',
     'admin_tools.dashboard',    
     # ...
     'django.contrib.auth',
     'django.contrib.sites',
     'django.contrib.admin'
     # ... and other applications ...
 )

3. python manage.py syncdb

4. connect to urls.py:

 urlpatterns = patterns ('',
     url (r '^ admin_tools /', include ('admin_tools.urls')),
     #... etc.  ...
 )

5. copy media files (js, css and images) in the MEDIA_ROOT project. Something like this - are you using virtualenv?

  cp -r / home / kmike / envs / nadovmeste / src / django-admin-tools / admin_tools / media / admin_tools / path / to / yourproject / media / 

You can instead make a symlink or use django-static-files .

Everything, you can go to the admin panel and be surprised at the terrible spotted hat how everything has changed. The installation, as you can see, is quite normal, no jung files need to be replaced (as in grappelli it was once).

Blocks


The most useful thing about django-admin-tools is, of course, blocks. You can configure which blocks and how to show on the title page, write your own blocks and take them ready.

1. Create a blank for the "dashboard":

python manage.py customdashboard

After that, the project dashboard.py file will appear in the project root. You can, in principle, write with your hands.

The file will contain 2 classes: CustomIndexDashboard and CustomAppIndexDashboard - for the main admin page and the individual application page. We will be interested in the CustomIndexDashboard class here.

2. Specify in settings.py that we will use our “dashboard” on the main admin page:

ADMIN_TOOLS_INDEX_DASHBOARD = 'yourproject.dashboard.CustomIndexDashboard'

3. For example: let's combine in one block the standard jung models from auth and our profile data, naming the block in Russian:

 from admin_tools.dashboard import modules, Dashboard

 class CustomIndexDashboard (Dashboard):

     def __init __ (self, ** kwargs):
         Dashboard .__ init __ (self, ** kwargs)
         self.children.append (
             modules.ModelList (
                 title = u'Users',
                 models = (
                     'django.contrib.auth. *',
                     'my_accounts.models.Profile',
                 ),
             )
         )


The model list contains full paths to the models you want to connect. All models that are listed in the models must be registered in the jung admin panel as usual. If there are several models, then you can not list them all, because there is support *. If you have included a bunch of models via * and you need to remove several of them, that is, the exclude parameter with a list of models that you do not need to show is symmetrical to the models parameter (there also has support *).

Standard blocks


django-admin-tools includes in the standard delivery several ready-made blocks for the dashboard. Detailed information about them can be found in the documentation . There will be just a brief overview + description of the wonderful modules.Group , which was not in the documentation at the time of this writing.

They should be imported as follows: from admin_tools.dashboard import modules

1. modules.ModelList - as it seems to me, this is the most basic and useful unit. Allows you to group arbitrary models within one block.

image

2. modules.Group is another super-useful block. Allows you to group any other blocks within yourself. The blocks can either be located one above the other, or in the form of an "accordion", or in tabs. Very convenient, for example, for graphs, or for separating the main and frequently used sections from the side.

image

Example of use:

         self.children.append (modules.Group (
             title = u "Statistics",
             display = "tabs",
             children = [
                 nadovmeste_modules.Overview (),
                 nadovmeste_modules.Subscribers (),
                 nadovmeste_modules.Finances (),
                 nadovmeste_modules.Users (),
             ]
         ))


In the list of children, you can specify any other blocks, incl. different types. In the example there are not standard modules from admin_tools.dashboard.modules, but your own (how to do them later).

There is no support for nested groups yet (or rather, js is not working correctly right now).

3. modules.LinkList - a list of arbitrary links. External links can be marked with a special icon, links can be placed both horizontally and in a column.

image

4. modules.AppList - in fact, an analogue of the main page of the standard admin panel. List of applications and models in them. I will tell you a secret, there is still not documented models parameter, which is absolutely similar to this parameter in modules.ModelList - with the difference that later the models will be grouped by applications. This block is useful if you want to add everything in a quick way, or repeat the usual djang admin panel - in other cases, it seems to me that ModelList is better suited. Although it may be a matter of taste.

5. modules.RecentActions - a list of recent actions, as in the standard Jangov admin panel. I do not know why he is needed :)

6. modules.Feed - allows you to show RSS feeds in the admin panel.

We write our blocks


Let's write, for example, an absolutely useless block that will simply output some message passed to it in the constructor.

1. Inherit from admin_tools.dashboard.modules.DashboardModule:

 class MyModule (modules.DashboardModule):
     def is_empty (self):
         return self.message == ''

     def __init __ (self, ** kwargs):
         super (MyModule, self) .__ init __ (** kwargs)
         self.template = 'my_blocks / hello.html'
         self.message = kwargs.get ('message', '')


2. Making templates / my_blocks / hello.html

 {% extends "admin_tools / dashboard / module.html"%}
 {% block module_content%}
     <h4> {{module.message}} </ h4>
 {% endblock%}


As you can see, the module object is passed to the template. This means that you can pass any data through its attributes (which we did in the example with message).

3. Connect the block to dashboard.py

  self.children.append (MyModule (title = u "Greeting", message = u'Hi! ')) 


All is ready. I think it is now clear how to do any more complex things, draw graphics in blocks, and so on. We prepare data, we make a template. If request is needed in the context of the template, then override the init_with_context method (by reference, an example).

The resulting block can be dragged with the mouse, collapsed, expanded, placed separately or in the tab of the modules.Group and so on. - on a par with standard blocks.

Yes, and if you need to just add something above or below the standard block, you can not do your own block / template, but use the attributes of the pre_content and post_content module.

Appearance


People have rather cautiously approached the design, and the new elements look quite organically alongside the standard jungle ones. But if you also don’t like the unnecessary header and the nightmarish spotted standard header from django-admin-tools, then it’s easiest to remove them using css. It would be nice to add a logo in the header. At the same time and with how to make threads, we will understand.
image

1. Making a css-file with your theme. For an example, you can see the theme that is used in our admin panel, another example is in the standard package .

2. Connect the file with the theme: in settings.py add the setting
  ADMIN_TOOLS_THEMING_CSS = 'css / theming.css' # path relative to MEDIA_ROOT 


What's next


The article is nothing about the menu, bookmarks, setting the actual dashboards, about the panels for individual applications. All this can be read in the documentation . If you want to help the project by connecting to the development - welcome . If something doesn't work in django-admin-tools, then I broke it all, if something is done great - write the news to David Jean Louis .

Little tricks


If you updated modules in your dashboard (added, deleted, swapped), and after that everything went somewhere, then delete the entries from DashboardPreferences.

The statistics in the screenshot - from the test server, the real data nadovmeste.ru not "burned." If interested, in the next article I will tell you how to fill the database with a large amount of test data, and how to get any graphs with statistics (as shown in the screenshot for 5 codes) (with the statistics on user registrations by day).

Yes, thank you obiwanus and leoglu for valuable comments at the stage of preparing the article.

Source: https://habr.com/ru/post/98539/


All Articles