Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding admin css in django

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?

like image 428
rajan sthapit Avatar asked Sep 09 '11 04:09

rajan sthapit


People also ask

How do I override CSS in Django?

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 .

How do I change the admin template in Django?

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.

Can CSS be used in Django?

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.


2 Answers

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:

  1. If you want to change the appearance of the admin in general you should override admin templates. This is covered in details here: Overriding admin templates. 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.
  2. If your style is model specific you can add additional styles via the 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',)         } 
like image 179
Torsten Engelbrecht Avatar answered Sep 19 '22 23:09

Torsten Engelbrecht


  • In settings.py, make sure your app is listed before admin in the INSTALLED_APPS.
  • Create (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 %} 
like image 22
tivnet Avatar answered Sep 21 '22 23:09

tivnet