Django admin custom pages
Tags: django
Published: 2022-07-17 07:26:22

---
title: Django admin custom pages
published: true
date: 2022-07-17 07:25:30 UTC
tags: Django
canonical_url: https://www.rockandnull.com/django-admin-custom-view/
---

 ![Django admin custom pages](https://images.unsplash.com/photo-1585750130716-42e238075a88?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDN8fGN1c3RvbXxlbnwwfHx8fDE2NTgwNDIxNzg&ixlib=rb-1.2.1&q=80&w=2000)

Django is great. And Django Admin is even greater. The amount of functionality provided with a few lines of code is mind-blowing.

But even if the amount of functionality you get in the admin interface is "for free", sometimes it might not be enough. Especially if you are building a site with medium to high complexity, it's almost sure that you will need a few custom admin pages.

As always, Django's documentation is super great. I always recommend checking that first. But it's a bit verbose and the parts for building custom admin pages are scattered throughout the doc.

This is a quick recap/cheatsheet on adding a custom admin page to your site.

## Create your own admin site

We will create a custom admin site that allows us to customize it however we want.

```
class CustomAdminSite(AdminSite): # 1.
    site_header = "Custom Admin Site header"
    site_title = "Custom Admin Site title"

    def get_urls(self): # 2.
        urls = super().get_urls()
        my_urls = [
            path('custom_admin_view/',
                self.admin_view(( # 3.
                CustomAdminView.as_view(admin_site=self))), name='cav'),
        ]
        return my_urls + urls # 4.

admin_site = CustomAdminSite(name='myadmin') # 5.
admin_site.register(MyModel)
```

_admin.py_

1. The new class needs to extend `AdminSite` ([documentation](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#customizing-the-adminsite-class))
2. Override the `get_urls()` method to provide all the pages of the admin site: the regular ones, and your own ([documentation](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls))
3. Don't forget to "wrap" any custom admin page with `self.admin_view()`. This provides the requirement for authentication for any custom page, exactly like any other admin page requires. Otherwise, it will be accessible without any protection! ([documentation](self.admin_viewhttps://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls))
4. Notice that custom admin pages are included before the regular admin URLs because the regular ones are quite permissive ([documentation](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls))
5. Create an instance of your own custom admin site and use that instance to register any models with the admin site (instead of the default admin site).

## Override the default admin site

If you are not planning on having multiple admin sites, it's better to replace the default admin site with your own custom one ([documentation](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#overriding-the-default-admin-site)).

```
from django.contrib.admin.apps import AdminConfig

class CustomAdminConfig(AdminConfig): # 1.
    default_site = 'myproject.admin.CustomAdminSite' # 2.
```

_apps.py_

```
INSTALLED_APPS = [
    ...
    'myproject.apps.CustomAdminConfig', # 3.
    ...
]
```

_settings.py_

```
admin_site.register(EquipmentCategory, EquipmentCategoryAdmin)
admin_site.register(EquipmentLocation, EquipmentLocationAdmin)
```

1. Create an app config that extends the default `AdminConfig`
2. Use the class of your own custom admin you created in the previous section
3. Replace the default `django.contrib.admin` app in the list of `INSTALLED_APPS`

## Create your own custom admin view

Finally, you can create your own custom admin view. These are regular views with some additional context items ([documentation](https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls)).

```
class CustomAdminView(View):
    admin_site = None # 1.

    def get(self, request):
        return render(request, 'custom_admin_view.html', dict(
            self.admin_site.each_context(request), # 1.
        ))

```

1. Pass a reference to your own custom admin site when instantiating this view (see the first section).
2. These are the common context items needed for the custom admin site to have the common fields, such as the `site_name`, `site_title`, etc (see the first section for these custom fields). 

Hopefully, you can now go and add your own custom admin pages easily and without confusion.

Happy coding!