0

Django version 3.2.4

I'm trying to change the default index_title, site_header, and site_title used by the admin site. I've tried all the suggestions here, but the login page refuses to use the updated values. In fact, the required context (site_header in the example below) is empty (confirmed by overriding the template and using the {{ debug }} variable as described here) and the "default" value of "Django administration" is being used. A line from the template C:\Program Files\Python38\Lib\site-packages\django\contrib\admin\templates\admin\base_site.html:

<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }} a</a></h1>

I currently have all three variables overwritten in my app's admin.py file, and the updated values are being used after I've logged in, but it doesn't work on the initial login page:

admin.site.site_header = 'My Site Header'
admin.site.index_title = 'My Index Title'
admin.site.site_title = 'My Site Title'

I can override the base_site.html template and hardcode a value, but I would like to understand why that is necessary and figure out how I can pass some context to that overwritten template. I've tried all the methods of overriding the original context described here to no avail. That includes having a custom admin site which overrides the default admin site. I've also noticed that the each_context method is not being called on the initial login page, but it is called after I have logged in and I'm viewing an admin page.

I think some of the problem may stem from how I have my urlpatterns set up in urls.py. I have it set up like this so the URL displayed in the address bar for a login is the same for both the admin and non-admin portions of the site.

urlpatterns = [
    path('', include('my_app.urls')),
    path('login', auth_views.LoginView.as_view(template_name='admin/login.html')),
    path('login/', auth_views.LoginView.as_view(template_name='admin/login.html')),
    path('logout', auth_views.LogoutView.as_view()),
    path('logout/', auth_views.LogoutView.as_view()),
    path('admin/login', generic_views.RedirectView.as_view(url='/admin', permanent=True, query_string=False)),
    path('admin/login/', generic_views.RedirectView.as_view(url='/admin', permanent=True, query_string=False)),
    path('admin/logout', auth_views.LogoutView.as_view()),
    path('admin/logout/', auth_views.LogoutView.as_view()),
    path('admin/', admin.site.urls)
]

I'm also exclusively using the django_auth_ldap.backend.LDAPBackend:

AUTHENTICATION_BACKENDS = [
    'django_auth_ldap.backend.LDAPBackend'
]

Any guidance would be appreciated.

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • Did also make your custom admin site the default admin site? https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#overriding-the-default-admin-site – schillingt Jun 28 '21 at 17:58
  • Yes, I performed the steps in both the "Customizing" and "Overriding" sections. – ubiquibacon Jun 28 '21 at 18:18

1 Answers1

0

I got around my problem by creating the following context processor:

from django.contrib import admin


def global_context(request):
    """
    This is intended to be a global context processor.  Any templates rendered from views using the
    `django.template.RequestContext` context (the default context used by generic views) will have this context
    available to them.
    """

    context = {
        'index_title': admin.site.index_title,
        'site_header': admin.site.site_header,
        'site_title': admin.site.site_title
    }

    return context

This in conjunction with setting admin.site.index_title, admin.site.site_header, and admin.site.site_title to the desired values in admin.py got things working as expected.

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179