I want to change certain css in admin django like base.css. Is it better to change directly in the django library? How can I override it in the best way?
Sometimes you can just extend the original admin file and then overwrite a block like {% block extrastyle %}{% endblock %} in django/contrib/admin/templates/admin/base. html as an example. If your style is model specific you can add additional styles via the Media meta class in your admin.py .
To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.
Including CSS in Django TemplateWe need to load the static files by adding the below line of code at the top of the template (. HTML) file. Then, insert the stylesheet in the HTML using link HTML tag. If you are new to the HTML and CSS, you can learn more about adding CSS in HTML code.
It depends a lot of what you want to do. Though first of all: do not overwrite it in the Django admin directly. You got two options I think are reasonable:
{% block extrastyle %}{% endblock %}
in django/contrib/admin/templates/admin/base.html
as an example.Media
meta class in your admin.py
. See an example here:class MyModelAdmin(admin.ModelAdmin): class Media: js = ('js/admin/my_own_admin.js',) css = { 'all': ('css/admin/my_own_admin.css',) }
settings.py
, make sure your app is listed before admin in the INSTALLED_APPS
.(your-app)/templates/admin/base_site.html
and put the <style>
block into the {% block extrahead %}
Example:
{% extends "admin/base_site.html" %} {% block extrahead %} <style> .field-__str__ { font-family: Consolas, monospace; } </style> {% endblock %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With