Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom view to django admin interface?

My django admin interface looks like this:

django-admin

Now I would like to add a view which does not correspond to a model.

I could overwrite the template of above page and add a custom link. But I think this would look ugly.

Example for overwriting admin/index.html:

{% extends "admin/index.html" %}

{% block content %}

{{ block.super }}
<div class="app-sonstiges module">
   ....
</div>
{% endblock %}

But maybe there is an official way to do add a custom view to the admin interface?

In my case I want to provide a form which can execute tcptraceroute to a remote server. The admin of my app needs this.

I used the same html tags. Now the link "tcptraceroute" looks nice, but unfortunately the messages moved down:

newest-actions-moved-down

Is there a way to get a custom part like "Sontiges ... tcptraceroute" like in the screenshot, without moving the latest actions down?

Here is how the html structure looks like. My <div class="app-sonstiges"> is below content-main:

my-part-after-content-main

like image 420
guettli Avatar asked May 22 '19 10:05

guettli


People also ask

How customize Django admin CSS?

In your static directory, create a static/admin/css/base. css file. Paste in Django's default Admin CSS first, then add your customizations at the bottom. If you do this, be sure to put your app BEFORE django.

Can we change Django admin template?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.


1 Answers

You have 3 options here:

Using third-party packages which provide menu

This is pretty straight forward, there are some good packages out there which support menu for admin, and some way to add your item to the menu.

An example of a 3rd party package would be django-admin-tools. The drawback is that it is a bit hard to learn. Another option would be to use django-adminplus but at the time of writing this, it does not support Django 2.2.

Hacking with django admin templates

You can always extend admin templates and override the parts you want, as mentioned in @Sardorbek answer, you can copy some parts from admin template and change them the way you want.

Using custom admin site

If your views are supposed to only action on admin site, then you can extend adminsite (and maybe change the default), beside from adding links to template, you should use this method to define your admin-only views as it's easier to check for permissions this way.

Considering you already extended adminsite, now you can use the previous methods to add your link to template, or even extend get_app_list method, example:

class MyAdminSite(admin.AdminSite):
    def get_app_list(self, request):
        app_list = super().get_app_list(request)
        app_list += [
            {
                "name": "My Custom App",
                "app_label": "my_test_app",
                # "app_url": "/admin/test_view",
                "models": [
                    {
                        "name": "tcptraceroute",
                        "object_name": "tcptraceroute",
                        "admin_url": "/admin/test_view",
                        "view_only": True,
                    }
                ],
            }
        ]
        return app_list

You can also check if current staff user can access to module before you show them the links. It will look like this at then end: enter image description here

like image 191
Pooya Mobasher Behrooz Avatar answered Oct 11 '22 19:10

Pooya Mobasher Behrooz