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.
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)
- The new class needs to extend
- Override the
get_urls()method to provide all the pages of the admin site: the regular ones, and your own (documentation)
- 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)
- Notice that custom admin pages are included before the regular admin URLs because the regular ones are quite permissive (documentation)
- 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).
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.
INSTALLED_APPS = [ ... 'myproject.apps.CustomAdminConfig', # 3. ... ]
admin_site.register(EquipmentCategory, EquipmentCategoryAdmin) admin_site.register(EquipmentLocation, EquipmentLocationAdmin)
- Create an app config that extends the default
- Use the class of your own custom admin you created in the previous section
- Replace the default
django.contrib.adminapp in the list of
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. ))
- Pass a reference to your own custom admin site when instantiating this view (see the first section).
- These are the common context items needed for the custom admin site to have the common fields, such as the
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.