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 (last updated Dec 2021, checked April 2023). The drawback is that it is a bit hard to learn. Another option would be to use django-adminplus (last updated Oct 2019, checked April 2023) 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:

Answer from Pooya Mobasher Behrooz on Stack Overflow
Top answer
1 of 5
32

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 (last updated Dec 2021, checked April 2023). The drawback is that it is a bit hard to learn. Another option would be to use django-adminplus (last updated Oct 2019, checked April 2023) 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:

2 of 5
10

Problem here that your div is not inside main-content. I suggest extending admin/index.html

Put in app/templates/admin/index.html

{% extends "admin/index.html" %}
{% load i18n static %}

{% block content %}
<div id="content-main">

{% if app_list %}
    {% for app in app_list %}
        <div class="app-{{ app.app_label }} module">
        <table>
        <caption>
            <a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">{{ app.name }}</a>
        </caption>
        {% for model in app.models %}
            <tr class="model-{{ model.object_name|lower }}">
            {% if model.admin_url %}
                <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
            {% else %}
                <th scope="row">{{ model.name }}</th>
            {% endif %}

            {% if model.add_url %}
                <td><a href="{{ model.add_url }}" class="addlink">{% trans 'Add' %}</a></td>
            {% else %}
                <td>&nbsp;</td>
            {% endif %}

            {% if model.admin_url %}
                {% if model.view_only %}
                <td><a href="{{ model.admin_url }}" class="viewlink">{% trans 'View' %}</a></td>
                {% else %}
                <td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
                {% endif %}
            {% else %}
                <td>&nbsp;</td>
            {% endif %}
            </tr>
        {% endfor %}
        </table>
        </div>
    {% endfor %}

    <!-- here you could put your div -->
    <div class="app-sonstiges module">
    ....
    </div>
    <!-- here you could put your div -->
{% else %}
    <p>{% trans "You don't have permission to view or edit anything." %}</p>
{% endif %}
</div>
{% endblock %}

The another approach is to add custom app to app_list. But it is far more ugly. So I suggest overriding template.

I assume you are overriding get_urls in AdminSite.

🌐
PyPI
pypi.org › project › django-admin-views
django-admin-views · PyPI
You simply subclass your admin from AdminViews instead of the standard admin.ModelAdmin. In this example we have a custom view that does nothing but redirect the user to CNN and a direct URL link that goes to my company’s homepage: from ...
      » pip install django-admin-views
    
Published   Nov 02, 2022
Version   1.0.3
Discussions

Django adding custom view in admin site - Stack Overflow
I am using Django 1.4. I would like to add a queue in admin page which should look like admin change_list page. I tried so many blogs and other forum, but did not get what I want. How can I add cus... More on stackoverflow.com
🌐 stackoverflow.com
python - Make new custom view at django admin - Stack Overflow
Sorry, I am still new at django. I want to make custom view at admin site that is not related to my model. I have read the documentation (https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#dj... More on stackoverflow.com
🌐 stackoverflow.com
March 8, 2018
How do I customise Django admin
Most people suggest making your own admin page if what you want to accomplish is fundamentally outside the scope of the existing admin page. I would recommend searching github for django projects that contain admin.StackedInline, admin.ModelAmin, admin.TabularInline these kinds of words indicate that the project is the customizing the admin page, and you'll see how unappealing the code is to work with. It's fine if you just want to add fields, make them editable, make them read only, but if you have a whole new idea for a different admin page, make your own admin page. More on reddit.com
🌐 r/django
10
2
September 9, 2024
[deleted by user]
Just write regular views. In my opinion, this is beyond the scope of what the built-in admin was intended for. You can do some customizations and template overrides in the admin, but you’re going to spend more time hacking around it than you would if you built your own views and templates. More on reddit.com
🌐 r/django
17
3
June 23, 2022
Top answer
1 of 4
21

This guide looks quite old. I would rather advise you to follow django docs.

someapp/admin.py

from django.contrib.admin import AdminSite
from django.http import HttpResponse

class MyAdminSite(AdminSite):

     def get_urls(self):
         from django.urls import path
         urls = super().get_urls()
         urls += [
             path('my_view/', self.admin_view(self.my_view))
         ]
         return urls

     def my_view(self, request):
         return HttpResponse("Hello!")

admin_site = MyAdminSite()

Source: https://github.com/django/django/blob/2.2/django/contrib/admin/sites.py#L194-L205

You should also update your project/urls.py and replace path('admin/', admin.site.urls) by path('admin/', admin_site.urls). Don't forget to from someapp.admin import admin_site before.

2 of 4
11

The guide you linked is old and I was surprised to not find anything directly answering your question in the last year or so.

  1. Create a new Admin Site in your app's admin.py or in a convenient place.
  2. Create a function in the new AdminSite that augments the get_urls() function with your extra urls.
  3. Make sure your project urls.py links to the newly created AdminSite.

The below works with Python 3.5.1 and Django 1.9.6.


my_app/admin.py

from django.contrib import admin
from django.contrib.admin import AdminSite
from django.http import HttpResponse

from my_app.models import SomeModel


class MyAdminSite(AdminSite):

    def custom_view(self, request):
        return HttpResponse("Test")

    def get_urls(self):
        from django.conf.urls import url
        urls = super(MyAdminSite, self).get_urls()
        urls += [
            url(r'^custom_view/$', self.admin_view(self.custom_view))
        ]
        return urls

admin_site = MyAdminSite()


@admin.register(SomeModel, site=admin_site)
class SomeModelAdmin(admin.ModelAdmin):
    pass

my_project/urls.py

from django.conf.urls import url, include

from my_app.admin import admin_site

urlpatterns = [
    url(r'^admin/', admin_site.urls),
    ...
]
🌐
GitHub
github.com › jsocol › django-adminplus
GitHub - jsocol/django-adminplus: Easily add custom views to the Django admin. · GitHub
AdminPlus offers a new function, admin.site.register_view, to attach arbitrary views to the admin: # someapp/admin.py # Assuming you've replaced django.contrib.admin.site as above.
Starred by 604 users
Forked by 78 users
Languages   Python 86.0% | Shell 7.8% | HTML 6.2%
🌐
TestDriven.io
testdriven.io › blog › customize-django-admin
Customizing the Django Admin | TestDriven.io
August 24, 2023 - This results in developers creating views and functions for things that could be easily implemented with a bit of admin site tweaking. In this article, we'll look at how to customize Django's admin site through practical examples.
🌐
GitHub
gist.github.com › rchrd2 › c1cabcbfbfb27295a22c
Adding custom views to django's admin · GitHub
Adding custom views to django's admin. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
gist.github.com › gpietro › 7d6f17f95ced52f24dbacd78d1d49bb6
Add a custom view in django admin · GitHub
Add a custom view in django admin. GitHub Gist: instantly share code, notes, and snippets.
Find elsewhere
🌐
Learnbatta
learnbatta.com › blog › how-to-add-custom-views-to-django-admin-91
How To Add Custom Views To Django Admin - learnBATTA
July 13, 2019 - from django.contrib import admin from django.http import HttpResponse from django.urls import path # my dummy model class DummyModel(models.Model): class Meta: verbose_name_plural = 'Dummy Model' app_label = 'sample' def my_custom_view(request): return HttpResponse('Admin Custom View') class DummyModelAdmin(admin.ModelAdmin): model = DummyModel def get_urls(self): view_name = '{}_{}_changelist'.format( self.model._meta.app_label, self.model._meta.model_name) return [ path('my_admin_path/', my_custom_view, name=view_name), ] admin.site.register(DummyModel, DummyModelAdmin)
🌐
Real Python
realpython.com › customize-django-admin-python
Customize the Django Admin With Python – Real Python
August 7, 2024 - You'll use AdminModel objects to add display columns, calculate values, link to referring objects, and search and filter results. You'll also use template overriding to gain full control over the admin's HTML.
🌐
DEV Community
dev.to › daiquiri_team › creating-a-custom-page-in-django-admin-4pbd
Creating a custom page in Django Admin - DEV Community
September 16, 2022 - We want customer information, a list of ordered products, and a total order sum on this page. Let's start with an empty template in products/templates/admin/products/order/detail.html. Django Admin enforces this structure for the templates: admin/APP_NAME/MODEL_NAME/some_template.html.
🌐
Webforefront
webforefront.com › django › admincustomlayout.html
Django admin custom page layout, data & behaviors
Listing 11-24 illustrates a Django admin class that uses a custom implementation of the changelist_view() method -- which adds custom data to access in the underlying Django admin change_list.html template -- as well as a custom implementation of the delete_view() method -- to execute custom logic when a delete action is taken on a Django admin class.
🌐
Vskills
vskills.in › home › creating custom admin views
Creating Custom Admin Views - Tutorial
April 12, 2024 - By extending admin/base_site.html, we get the look and feel of the Django admin “for free.” Figure 17-2 shows what the end result looks like. Figure 17-2. A custom “books by publisher” admin view
🌐
Dev.Junction
blog.devjunction.in › django-admin-customization-adding-custom-views-in-django-admin
Django Admin Customization: Adding Custom Views in Django admin
December 25, 2022 - This can be useful for tasks that ... a custom view in Django Admin, you'll need to define a function and decorate it with @admin.site.admin_view or @admin.site.login_required....
Top answer
1 of 2
2

Rather then creating a custom admin change view I would approach this problem by extending the built in one, the django admin provides a number of hooks to facilitate this, here are my thoughts:

  1. Modify the default queryset but I would recommend against this as you loose the ability to edit objects not returned by the queryset.
  2. Create a custom filter that would display your 'broker list' queryset - I recommend this one as it further will give you a GET url query you can link to directly to activate this filter.
  3. You can even completely gut out the change view and use your own - this option I have the least experience with and can not comment.

You may wish to also take at look changing the change list template and admin custom actions to further customize the look and feel and to provide custom 'actions'; I have used both successfully in the past to provide project specific functionality.

Very exciting are the has_add_premission, had_change_permission, and has_delete_permission hooks, couple these with like django-guradian and custom admin base template could allow you to use the backend admin as a complete front end administration.

Do take the time to throughly read the entire model admin page - the better I know it the less I find myself coding custom front end administration.

2 of 2
1

If you want a custom view to be shown at the admin site, you can use "django admin plus" Its a library that allows you to bind custom views to admin site. You can use queries and models of your choice in that view and go on further as it was'nt admin site.

Try this django library and do share with us your feedback.

Your view should look something like this after registering it to admin.py

@admin.site.register_view('pathname')
def my_view(request):
    do_something

Do share your experience with this library.

Have a good one

🌐
Adrienne Domingus
adriennedomingus.com › tech › adding-custom-views-and-templates-to-django-admin
Adding Custom Views and Templates to Django Admin - Adrienne Domingus
Your admin site has a get_urls method that doesn’t need overwriting if you just want a page for each admin page you’ve registered. But adding our own custom view to preview our template is a great example of where we’ll need to add to this: from django.conf.urls import url from django.contrib import adminclass CustomAdminSite(admin.AdminSite): def get_urls(self): urls = super(CustomAdminSite, self).get_urls() custom_urls = [ url(r'desired/path$', self.admin_view(organization_admin.preview), name="preview"), ] return urls + custom_urls
🌐
Medium
adriennedomingus.medium.com › adding-custom-views-or-templates-to-django-admin-740640cc6d42
Adding Custom Views and Templates to Django Admin | by Adrienne Domingus | Medium
December 9, 2018 - Adding Custom Views and Templates to Django Admin The Django admin feature is great for quickly spinning up basic CRUD actions without giving someone direct database access, but gives you very little …
🌐
Rock and Null
paleblueapps.com › rockandnull › django-admin-custom-view
Django admin custom pages - Pale Blue
July 17, 2022 - Replace the default django.contrib.admin app in the list of INSTALLED_APPS · Finally, you can create your own custom admin view.
🌐
Django
docs.djangoproject.com › en › 3.1 › ref › contrib › admin
The Django admin site | Django documentation | Django
The admin has many hooks for customization, but beware of trying to use those hooks exclusively. If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views. In this document we discuss how to activate, use, and customize Django...
🌐
Django
django.how › admin › admin-custom-view
Admin Custom View Django.How
class ListingAdmin(admin.ModelAdmin): list_display = ('id', 'title', 'is_published', 'price', 'list_date', 'realtor') list_display_links = ('id', 'title') list_filter = ('realtor',) list_editable = ('is_published','price') search_fields = ('title', 'description', 'city', 'state') # readonly_fields = ["name"] list_per_page = 25 admin.site.register(Listing, ListingAdmin)