How to Customize the Django Admin Panel: A Complete Guide

Posted: November 03, 2024 | Updated: November 03, 2024

 1.Change the Site Header and Title

Create a new file called admin.py in your app directory (if it doesn’t already exist) where the home page is configured and add the following code:

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

admin.site.site_header = _("My Custom Admin Header")
admin.site.site_title = _("My Custom Admin Title")
admin.site.index_title = _("Welcome to My Custom Admin")

Now check your Admin Panel title.

2. Add favicon to the Django admin panel

Adding a favicon to the Django admin panel involves a few simple steps. Here’s how you can do it:

Step 1: Prepare Your Favicon and Place the Favicon in Your Static Files
Create a Favicon: Ensure your favicon is in the correct format, usually .ico, but you can also use .png or .svg.

Name Your Favicon: For convention, name your favicon file favicon.ico.

Create a Static Directory: If you don’t already have a static directory in your Django app, create one. The structure should look like this:

myapp/
    static/
        myapp/
            favicon.ico

Move Your Favicon: Place your favicon.ico file in the myapp/static/myapp/ directory.

Update Your Admin Template

To include the favicon in the Django admin, you will need to override the admin base template.

Create a Templates Directory: In your app, create a directory structure like this:

myapp/
    templates/
        admin/
            base_site.html

Copy the Base Template: Copy the original Django admin base template into your new admin directory. You can find the default template in the Django source code, or create a new base_site.html file with the following content:

{% extends "admin/base.html" %}
{% load static %}

{% block extrastyle %}
    <link rel="icon" href="{% static 'myapp/favicon.ico' %}" type="image/x-icon">
    {{ block.super }}
{% endblock %}

Configure Settings Ensure your settings are configured to find static files:

Open your settings.py file and ensure you have the following settings:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / "static",  # or your static files path
]

Collect Static Files (for Production) If you’re running in production, make sure to collect static files:

python manage.py collectstatic

Restart the Server

After making these changes, restart your Django development server:

python manage.py runserver

3. Customizing Admin Models

You can further customize how your models are displayed in the admin. Use the ModelAdmin class to specify fields and settings for your models.

In your admin.py, you can do something like this:

from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2', 'created_at')
    search_fields = ('field1', 'field2')
    list_filter = ('status',)

admin.site.register(MyModel, MyModelAdmin)

This example configures the admin interface for MyModel to show specific fields in the list view, enable search functionality, and provide filters.

© 2024 Webapptiv. All rights reserved.