How to Override CSS in Django Admin?
Last Updated :
31 Jul, 2024
Django's admin interface is a powerful tool for managing application data. It comes with a default styling that may not always fit the visual identity of your project. Customizing the admin interface can help create a more cohesive user experience. In this article, we'll walk you through the process of overriding the CSS in Django's admin panel by creating a small Django project.
How to Override CSS in Django Admin?
Below are the guide of How to Override CSS in Django Admin.
1. Setting Up the Django Project
First, let's create a new Django project and application. Open your terminal and run the following commands:
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
2. Configuring the Project
Update settings.py in the myproject directory to include the new app and configure static files:
Python
# myproject/settings.py
INSTALLED_APPS = [
...
'myapp',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
File Structure
3. Creating a Custom CSS File for Overriding in Django Admin
Create a directory for static files in your app:
mkdir -p myapp/static/myapp/css
Create a custom CSS file myapp/static/myapp/css/custom_admin.css:
CSS
/* myapp/static/myapp/css/custom_admin.css */
body {
background-color: rgb(71, 6, 248);
}
#header {
background-color: #4CAF50;
color: gray;
}
5. Overriding the Admin Template
To apply your custom CSS, you need to extend the admin base template. Create a new directory for templates:
mkdir -p myapp/templates/admin
Create a new template myapp/templates/admin/base_site.html:
HTML
{% extends "admin/base_site.html" %}
{% load static %}
{% block extrahead %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'myapp/css/custom_admin.css' %}">
{% endblock %}
6. Applying the Changes urls.py and admin.py
Ensure your project is set up to serve static files during development by adding the following to urls.py:
Python
# myproject/urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In myapp/admin.py, the MyModelAdmin class extends admin.ModelAdmin and uses the Media inner class to include custom CSS. The css attribute specifies that custom_admin.css from myapp/css should be applied to all admin pages for this model. This allows for the customization of the admin interface's appearance
admin.py
Python
# myapp/admin.py
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class Media:
css = {
'all': ('myapp/css/custom_admin.css',)
}
7. Running the Project
Run the development server to see the changes:
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
Log in to the admin panel at http://127.0.0.1:8000/admin/ using the superuser credentials you created. You should see the customized admin interface with your custom CSS applied.
Conclusion
Overriding CSS in Django's admin interface allows you to tailor the look and feel to better match your project's design requirements. By following the steps outlined above, you can create a more cohesive and visually appealing admin panel. This customization not only improves aesthetics but can also enhance user experience and productivity.
Similar Reads
How to Reset Django Admin Password?
Forgetting or needing to reset the admin password in Django is a common issue that can be resolved in a few easy steps. Django provides several ways to reset the admin password, depending on the situation. In this article, we'll explore multiple methods to help you recover or reset the password for
2 min read
Python | Django Admin Interface
Prerequisites: Django Introduction and Installation Creating a ProjectThe Django Admin Interface is one of the most powerful features of the Django framework. It provides a ready-to-use interface for managing project data through models, allowing developers and site administrators to perform Create,
3 min read
How to create superuser in Django?
Django comes with a built-in admin panel that allows developers to manage the database, users and other models efficiently. This eliminates the need for creating a separate admin interface from scratch. To access and manage the Django Admin panel, we need to create a superuser i.e. a user with full
2 min read
Adding CSP headers in Django Project
Website security has been an important factor while developing websites and web applications. Many frameworks come with their own security policies and developers also try to implement the utmost security policies while developing their applications. Still even after this much hard work hackers will
5 min read
How to Create an App in Django ?
In Django, an app is a web application that performs a specific functionality, such as blog posts, user authentication or comments. A single Django project can consist of multiple apps, each designed to handle a particular task. Each app is a modular and reusable component that includes everything n
3 min read
How to Query as GROUP BY in Django?
In Django, the powerful ORM (Object-Relational Mapping) allows developers to interact with databases using Python code. One common database operation is the GROUP BY query, which groups rows sharing a property so that aggregate functions can be applied to each group. This article will guide you thro
3 min read
How to use Tailwind CSS with Django ?
Tailwind CSS has gained immense popularity among developers for its utility-first approach to styling web applications. Django on the other hand is a robust and flexible web framework written in Python. Combining these two powerful tools can enhance your Django projects. In this article, we will exp
4 min read
Customize Django Admin Interface
Django admin by default is highly responsive GUI, which offers various features and an overall CRUD application to help developers and users. Moreover, Django admin can be customized to fulfill one's needs such as showing fields on the home page of the table, etc. In this article, we will discuss ho
3 min read
How to create a Django project?
Dive into the world of web development with Python by exploring the versatile Django framework. Django is a go-to for many developers due to its popularity, open-source license, and robust security features. It enables fast and efficient project development. In this tutorial, we will guide you throu
5 min read
How to Add CSS in Ruby on Rails?
This article focuses on discussing how to add Cascading Style Sheets (CSS) in Ruby on Rails. Table of Content Using SCSSUsing Plain CSSUsing Inline StylesUsing External CSS FrameworksComparison of Different ApproachesAdditional ConsiderationsConclusionUsing SCSSSCSS (Sass) is a preprocessor that ext
4 min read