3

I am using django-livesettings to save the site title to the database. To be able to access a config value, however, you need to pass the variable to the template via a view:

http://django-livesettings.readthedocs.org/en/latest/usage.html#accessing-your-value-in-a-view

What method of the admin.ModelAdmin class can I override to pass variables to the base_site.html, where the admin site title "Django Site Admin" is located?

This answer may come close but I don't know what it misses: Django how to pass custom variables to context to use in custom admin template?

Community
  • 1
  • 1
yretuta
  • 7,963
  • 17
  • 80
  • 151
  • How about custom [context processor](https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext) that will add livesettings variables that you need to template context? – draganHR Jun 20 '12 at 12:21

2 Answers2

2

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 &copy; MyCo</small></p>
</div>
{% endblock %}
Agoun
  • 334
  • 3
  • 11
0

The second link is about adding extra variable to the changelist page of a ModelAdmin.
The base_site.html is extended by many admin pages and thus views, if you want some global change, you need to either extends context of all the relative views or, override the base_site.html itself by setting a variable in the context

okm
  • 23,575
  • 5
  • 83
  • 90
  • which view should I add the context variable in? – yretuta Jun 16 '12 at 20:28
  • @Ygam it depends on your requirement: in which admin page the customization takes effect. You could check `django.contrib.admin.sites.get_urls()` and `django.contrib.admin.options.get_urls()` to find out where the actual admin views reside in. Views such as `index`, `change_view`, `change_list` normally provide API(`extra_context=None` in signature) for adding extra context. You could extend `AdminSite` and `ModelAdmin` to achieve it. If your customization is global, just override the `base_site.html` by copying it to a directory w/ higher priority during template loading, and modify it. – okm Jun 18 '12 at 16:00