Overriding admin css in django

Multi tool use
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?
8 Answers
8
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 %}
django/contrib/admin/templates/admin/base.html
Media
admin.py
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ('js/admin/my_own_admin.js',)
css = {
'all': ('css/admin/my_own_admin.css',)
}
None I am aware of. The django admin is in the end nothing else than a django reusable app. That's the way to go with any other reusable app as well.
– Torsten Engelbrecht
Sep 9 '11 at 6:54
I notice there's a .css file listed within the js here... does that actually work? I can't get it to.
– fastmultiplication
Jul 18 '12 at 9:03
Yes, you are right. I made a mistake and edited my answer with an example from the django docs.
– Torsten Engelbrecht
Jul 20 '12 at 17:01
I just extended admin/base.html to include a reference to my own css file - at the end. The beauty of css is that you don't have to touch existing definitions, just re-define.
In your static directory, create a static/admin/css/base.css
file.
static/admin/css/base.css
Paste in Django's default Admin CSS first, and adjust as you see fit.
If you do this, be sure to put your app BEFORE
django.contrib.admin
in the list of INSTALLED_APPS
. If you don't, collectstatic will find the admin base.css first and your customized version won't overwrite it.– Dave
Jul 31 '15 at 22:40
django.contrib.admin
INSTALLED_APPS
This isn't a good long term solution. It copy/pastes a bunch of code and it won't be maintained as Django upgrades.
– mlissner
Jul 26 '17 at 16:24
Any styling to the Django admin is basically a fork of the code. An update to Django could in effect break your customizations. My recommendation is to keep customizations to a minimum and add them at the bottom of Django's default styling. Then you can manually update the default styling if and when you want to.
– Ryan Allen
Jul 27 '17 at 20:04
@danny-w-adair's answer above is better; keeps things "DRY", but it's still a fork of Django's code that could lead to minor upgrade pains.
– Ryan Allen
Jul 27 '17 at 20:12
This solution will work for the admin site, I think it's the cleanest way because it overrides base_site.html
which doesn't change when upgrading django.
base_site.html
Create in your templates directory a folder called admin
in it create a file named base_site.html
.
admin
base_site.html
Create in your static directory under css
a file called admin-extra.css
.
css
admin-extra.css
Write in it all the custom css you want for your forms like: body{background: #000;}
.
body{background: #000;}
Paste this in the base_site.html
:
base_site.html
{% extends "admin/base.html" %}
{% load static from staticfiles %} # This might be just {% load static %} in your ENV
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "css/admin-extra.css" %}" />{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>
{% endblock %}
{% block nav-global %}{% endblock %}
That's It! you're done
Thanks, works great! If it doesn't work, make sure your app is before the admin app in
INSTALLED_APPS
, otherwise your template doesn't override django's.– Dirbaio
Oct 12 '16 at 20:21
INSTALLED_APPS
settings.py
INSTALLED_APPS
(your-app)/templates/admin/base_site.html
<style>
{% block extrahead %}
Example:
{% extends "admin/base_site.html" %}
{% block extrahead %}
<style>
.field-__str__ {
font-family: Consolas, monospace;
}
</style>
{% endblock %}
Best answer. If you want you can also avoid to list your app before admin in INSTALLED_APPS by putting a "template" directory in base project and referencing it in settings.py: TEMPLATES = [ ...other stuffs..., 'DIRS': [os.path.join(BASE_DIR, 'templates')], ]
– RedPelle
Mar 11 at 23:34
Perhaps grappelli will save you the bother...
http://grappelliproject.com/
Old and out of date...
https://lincolnloop.com/blog/customizing-django-admin-eurodjangocon-2009/
404 not found in July 2018
– Andrew Swift
Jul 2 at 15:41
@AndrewSwift fixed.
– John Mee
Jul 3 at 1:19
Have admin/css/changelists.css
inside a folder in STATICFILES_DIRS
, and it will user that changelists.css instead of the default admin one.
admin/css/changelists.css
STATICFILES_DIRS
If you want a global scope and you don't want to think about overriding templates a mixin works really well for this. Put this code wherever you want:
class CSSAdminMixin(object):
class Media:
css = {
'all': ('css/admin.css',),
}
Then, make a CSS file called admin.css
with your overrides, for example:
admin.css
select[multiple] {
resize: vertical;
}
Then, in whatever models you want, do:
class MyModelAdmin(admin.ModelAdmin, CSSAdminMixin):
And you'll be all set.
I like your answer, but why not to add directly to MyModelAdmin
– Goran
Oct 13 '17 at 14:10
You could do that if it's one model, but it'll get messy if you do it to many models.
– mlissner
Oct 13 '17 at 14:24
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Actually, it's not model level but the whole site itself. To be specific changes in base.css,ie.css etc. One option is to include the admin/base.html in my application itself and use my custom base.css in the admin/base.html file. That way I will have to include some template files from the django admin to my own site. Is there any better solution than this ?
– rajan sthapit
Sep 9 '11 at 5:35