4

I want to replace dynamically "Site administration" by a custom string in my admin. I've already overridden "base.html" for some other purpose, but now I need to pass a variable to this template to replace {{ title }} in

{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}

I've seen from this question that a variable can be passed to the change list template by overriding changelist_view and adding an extra_context in the model admin, but how can I pass an extra context to the "main" page of the admin"?

Community
  • 1
  • 1
jul
  • 36,404
  • 64
  • 191
  • 318

2 Answers2

6

The index() view is inside django.contrib.admin.site.AdminSite class and supports extra_context as well, you could override it, something like:

def index(self, *args, **kwargs):
     return admin.site.__class__.index(self, extra_context={'title':'customized title'}, *args, **kwargs)
admin.site.index = index.__get__(admin.site, admin.site.__class__)

Also you could override AdminSite directly and use customized_site instead of admin.site:

class CustomizedAdminSite(AdminSite):
    def index(self, *args, **kwargs):
        return super(CustomizedAdminSite, self).index(extra_context={...}, *args, **kwargs)
customized_site = CustomizedAdminSite()

If you want to have title in all Admin pages, better to use context processor or customize some template tag if you can.

okm
  • 23,575
  • 5
  • 83
  • 90
1

You override the "admin/base_site.html" template:

{% extends "admin/base.html" %}
{% load i18n %}

{% block title %} {{ title }} | {% trans 'YOUR TITLE HERE' %} {% endblock %}

{% block branding %}
<h1 id="site-name">{% trans 'STUFF HERE PERHAPS' %} </h1>
{% endblock %}

{% block nav-global %}

{% endblock %}
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164