Home > Web Front-end > CSS Tutorial > How can I customize CSS styles in the Django admin interface without modifying the base.css file directly?

How can I customize CSS styles in the Django admin interface without modifying the base.css file directly?

Susan Sarandon
Release: 2024-11-04 11:53:02
Original
471 people have browsed it

How can I customize CSS styles in the Django admin interface without modifying the base.css file directly?

Overriding CSS in Django Admin: Best Practices

Problem:

You want to customize certain CSS styles in the Django admin interface, particularly those in the base.css file. However, you're not sure whether to modify the styles directly in the Django library or to override them elsewhere.

Answer:

Overwriting styles directly in the Django library is generally discouraged. Here are two recommended approaches:

1. Override Admin Templates:

If you need to modify the overall appearance of the admin interface, you can override Django's admin templates. Refer to the documentation on Overriding Admin Templates for detailed instructions. For specific style changes, you can extend the original admin template and override blocks like {% block extrastyle %}{% endblock %} in django/contrib/admin/templates/admin/base.html.

2. Use Admin Media Meta Class:

For model-specific style changes, you can use the Media meta class in your admin.py file. This allows you to add custom styles and JavaScript files. Here's an example:

<code class="python">class MyModelAdmin(admin.ModelAdmin):
    class Media:
        js = ('js/admin/my_own_admin.js',)    
        css = {
            'all': ('css/admin/my_own_admin.css',)
        }</code>
Copy after login

The above is the detailed content of How can I customize CSS styles in the Django admin interface without modifying the base.css file directly?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template