Home >Web Front-end >CSS Tutorial >How can I customize CSS styles in the Django admin interface without modifying the base.css file directly?
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>
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!