Freelancing for Pale Blue

Looking for flexible work opportunities that fit your schedule?

Django admin custom pages

Django Jul 17, 2022

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 = [
                self.admin_view(( # 3.
                CustomAdminView.as_view(admin_site=self))), name='cav'),
        return my_urls + urls # 4.

admin_site = CustomAdminSite(name='myadmin') # 5.
  1. The new class needs to extend AdminSite (documentation)
  2. Override the get_urls() method to provide all the pages of the admin site: the regular ones, and your own (documentation)
  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)
  4. Notice that custom admin pages are included before the regular admin URLs because the regular ones are quite permissive (documentation)
  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).

from django.contrib.admin.apps import AdminConfig

class CustomAdminConfig(AdminConfig): # 1.
    default_site = 'myproject.admin.CustomAdminSite' # 2.
    'myproject.apps.CustomAdminConfig',  # 3.
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).

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!


Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.