In Django grappelli, how can I add my own css files to all the admin pages? Or is there a way to extend admin's base.html
template?

- 461
- 6
- 19
-
Check this post: http://stackoverflow.com/questions/31135581/can-i-edit-the-django-administration-screen-to-display-my-html – Alon Weissfeld Aug 06 '15 at 08:03
-
@AlonWeissfeld not exactly what is need, but thanks. – Vladimir Chub Aug 06 '15 at 09:40
4 Answers
- 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.
- 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',)
}

- 469
- 2
- 8
-
I created a template project/admin/templates/admin/base.html and added next: `{% extends 'admin/base.html' %} {% block extrastyle %} {% endblock %} ` but there are no changes in admin after my actions. – Vladimir Chub Aug 06 '15 at 08:10
-
This was what I needed. Thanks for showing the way for model specific additional css. – alierdogan7 Aug 15 '19 at 06:48
I've stumbled upon the same problem a while ago, but I can't remember where I have found this solution.
If you don't want to include additional dependencies in your project, you can use a little hack: Obviously, you can't just override the admin/base.html
template and tell it to extend admin/base.html
, because that would reference to itself.
You can, however, add another template directory that would point to grappelli module location (instead of the templates
location). The settings would look something like this:
import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [
'your/normal/templates/path',
os.path.dirname(grappelli.__file__)
]
},
]
This way you can create a admin/base.html
template, which will extend templates/admin/base.html
- the latter being the grappelli base template for the admin interface.

- 2,870
- 16
- 28
-
Beautiful solution, but 'DIRS' param released only since Django 1.8 – Vladimir Chub Aug 06 '15 at 10:35
-
It will work with TEMPLATE_DIRS in pre-1.8 django versions: https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs - the whole TEMPLATES settings I pasted is just to give context of where the setting should be put. – samu Aug 06 '15 at 11:14
-
I found the complex solution. First of all, I found a python package which can help you to tell the Django Template Loader that your template has a highest priority in comparison with others.
pip install django-templateloaderwithpriorities
In your django settings file put next code:
TEMPLATE_LOADERS = ('templateloaderwithpriorities.Loader', ) + TEMPLATE_LOADERS TEMPLATE_LOADER_PRIORITIES = ['project/admin/templates']
- In
project/static/css/admin/extra_admin.css
put your css. In
project/admin/templates/admin/base_site.html
(not inbase.html
!) put the next code:{% extends 'admin/base.html' %} {% load staticfiles %} {% block javascripts %} {{ block.super }} <script>alert('OK')</script> {% endblock %} {% block stylesheets %} <link rel="stylesheet" href="{% static 'css/admin/extra_admin.css' %}"> {{ block.super }} {% endblock %}
- That's all! When you go to your site admin page, you see the javascript alert "OK", so in my example I've also included a custom js.
Everything also works fine with Django Grappelli.

- 461
- 6
- 19
The admin 'base.html' template can be extended by making an admin directory inside the main app folder. Inside the admin directory create a new file named base.html. Copy these lines in new files.
{% extends 'admin/base.html' %}
{% load staticfiles %}
{% block javascripts %}
{{ block.super }}
<script type="text/javascript" src="{% static 'app/js/action.js' %}"></script>
{% endblock %}
{% block stylesheets %}
{{ block.super }}
{% endblock %}
Here, I have overridden my javascript using an new script.

- 129
- 1
- 3