layers Introducing Unfold components - Pre-made HTML blocks in Django admin for custom dashboards arrow_forward

Loading custom Django admin site

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.

  • calendar_month 11.4.2023
  • pace 15 minutes

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

Be first to know about new features and updates

Each time something new happens in Unfold, like new major features or new design components, we'll send you a newsletter. We promise not to spam you.

Django admin theme built with Tailwind CSS to bring modern look and feel to your admin interface. Already contains several built-in features for smooth developer experience.

© 2023 - 2024 Created by unfoldadmin.com. All rights reserved.