I have solved this in a cack-handed way and would like to learn how to
do it correctly.
I had trouble getting my own css files to work in the admin overriding
the contrib.admin styles because the admin base.css kept being used
instead of mine when I used the extrastyle block. Here is my
base_site.html template ...
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {% trans 'X' %}{% endblock %}
{% block extrastyle %}{% endblock %}
{% block userstyle %}<link rel="stylesheet" type="text/css"
href="/static/css/darker.css" />{% endblock %}
{% block branding %}
<h1 id="site-name">{% trans 'X Administration' %}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
{% block extrahead %}{% endblock %}
You can see the "foreign" userstyle block there. That is where I had to
put my css to get it used in the right cascade sequence over the top of
the real thing. I think the extrastyle block is for extra styles rather
than replacement styles.
I wrote a script to patch the admin base.html to insert that userstyle
block and that works. But adjusting the admin base.html is not nice.
Hence I would like to discover how it should be done.
Here is my patch_admin_base.py script ...
"""
patch django-admin base.html to include a userstyle block
"""
project = "x"
srvroot = "/srv/www"
vws_root = "%s/%s" % (srvroot, project)
app_root = "%s/%s" % (vws_root, project)
import os
import sys
if app_root not in sys.path:
sys.path.insert(0, app_root)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "%s.settings" % project)
from django.contrib import admin
flag = '{% block extrastyle %}{% endblock %}'
patch1 = '{# userstyle inserted by md #}\n'
patch2 = '{% block userstyle %}{% endblock %}\n'
base_template = 'templates/admin/base.html'
done = False
pth =
os.path.split(str(admin).split()[-1].split("'")[-2].replace("\\","/"))[0]
template = '%s/%s' % (pth, base_template)
if os.path.isfile(template):
lines = list()
with open(template, "r") as base:
lines = base.readlines()
done = patch2 in lines
if not done:
with open(template, "w") as base:
for line in lines:
base.write(line)
if flag in line:
base.write(patch1)
base.write(patch2)
done = True
if done:
print('\n%s patched' % template)
else:
print('\n%s not patched' % template)
else:
print('\n%s already patched' % template)
else:
print('\nbase.html not found')
>
> Thanks very much for any help with this!
>