The most elegant solution I found is here:
https://stackoverflow.com/a/36421610/7521854
Essentially, it overrides your admin site's each_context
method to add as many custom variables as required. The updated context is applied to all admin pages without any further effort.
In my case, I want to have a custom footer displaying release version info. This info is taken from a file which is automatically updated with a git describe
command during deployment.
File: app-name/sites.py
:
class MyAdminSite(AdminSite):
""" Extends the default Admin site. """
site_title = gettext_lazy('My Admin')
site_header = gettext_lazy('My header')
index_title = gettext_lazy('My Administration')
def each_context(self, request):
version_info = ""
try:
version_info = os.environ['RELEASE_TAG']
except KeyError:
f = open(os.path.join(settings.BASE_DIR, 'assets/version.txt'), 'r')
version_info = f.read()
f.close()
os.environ['RELEASE_TAG'] = version_info
context = super(MyAdminSite, self).each_context(request)
context['releaseTag'] = version_info
return context
admin_site = MyAdminSite(name='my_custom_admin')
And the related footer tag is:
{% block footer %}
<div class="copyright-center">
<p><small>My Admin {{releaseTag}} Copyright © MyCo</small></p>
</div>
{% endblock %}