It is very easy to create an administration section for the website by implementing a few lines of code. Adding the new app into INSTALLED_APPS list, registering custom models and you are ready to go with full-fledged administration. But in most cases, it is needed to add at least a bit of custom code.
It is very easy to create an administration section for the website by implementing a few lines of code. Adding a new app into INSTALLED_APPS list, configure root urls.py, register custom models and you are ready to go with full-fledged administration. But in most cases, it is needed to add at least a bit of custom flavor and override certain parts of administration.
Before we start, we show off how to incorporate admin into the application. In general your need to do two things. Adding new application into INSTALLED_APPS list and configure urls.py with custom admin URLs.
# settings.py INSTALLED_APPS = [ 'django.contrib.admin', # Other applications ]
# urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ # Other links path('admin/', admin.site.urls), ]
Above you can see the minimally needed admin configuration. The new admin area is available under URL defined in urls.py and it is possible to sign in. But if we want to, for example, change the admin's title, it is not possible without adding custom code.
To achieve custom title, we need to define custom AppConfig for administration and then in this AppConfig we will load our new AdminSite with overriden title. Just for quick clarification, AppConfig contains application meta information. AdminSite class encapsulated business logic and behavior of the administration panel.
# apps.py from django.contrib.admin.apps import AdminConfig class AppAdminConfig(AdminConfig): default_site = 'app.admin.AppAdminSite'
# admin.py from django.contrib.admin import AdminSite from django.utils.translation import ugettext_lazy as _ class AppAdminSite(AdminSite): site_title = _('Hello World') index_template = 'app/dashboard.html' def index(self, request, extra_context=None): # Update extra_context with new variables return super().index(request, extra_context)
In the example code, you can see the new class was created by inheriting from AdminSite. It is possible to change attributes or override methods. The most common scenario is changing site_title or overriding index_template for custom dashboard template.
The last thing which is missing is to change the root urls.py and load new AppAdminConfig created in the previous step. Then comment the line containing django.contrib.admin because it is not needed anymore.
# settings.py INSTALLED_APPS = [ # 'django.contrib.admin', 'app.apps.AppAdminConfig', # Other applications ]
Obviously, there are other ways how the default AdminSite can be overridden. For example, we can override AdminSite through the urls.py where the admin's attributes like site are directly changed. But the solution above looks more maintainable and future-proof. Below you can see final files structure.
| app |- __init__.py |- admin.py |- apps.py |- settings.py |- urls.py
Did you decide to start using Unfold in your application but you need help with integration? Feel free to get in touch for consulting or development services.
* The cost of service starts from €329 EUR.