Opened 3 years ago

Closed 3 years ago

Last modified 2 years ago

#32681 closed Bug (fixed)

Variable lookup errors are logged rendering the admin index page if subtitle is not defined.

Reported by: Zain Patel Owned by: Zain Patel
Component: contrib.admin Version: 3.2
Severity: Release blocker Keywords: admin, template
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Zain Patel)

This looks like a bug introduced in Django 3.2 with the introduction of a new variable introduced in the base templates (django/contrib/admin/templates/base_site.html) named subtitle. This variable is passed in most places within the admin site, e.g in django/contrib/admin/options.py

Loading the admin index page with log-level debug shows the following exception/stack trace (that admittedly does not affect anything functional - simply clutters the logs):

Exception while resolving variable 'subtitle' in template 'admin/index.html'.
Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
  File "...lib/python3.7/site-packages/django/template/context.py", line 83, in __getitem__
    raise KeyError(key)
KeyError: 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 835, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'RequestContext' has no attribute 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 843, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 850, in _resolve_lookup
    (bit, current))  # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [subtitle] in

I believe this can be fixed by providing subtitle: None in the AdminSite.each_context` method to default it to that. I am happy to create a PR for this.

On second though - instead of hardcoding subtitle to None - potentially it should be a customisable option in the AdminSite itself, so:

class MySite(AdminSite):
  site_header = "blah"
  subtitle = "my subtitle"

?

Change History (14)

comment:1 by Zain Patel, 3 years ago

Description: modified (diff)

comment:2 by Zain Patel, 3 years ago

Description: modified (diff)

comment:3 by Zain Patel, 3 years ago

Potential fix as described in my ticket here: https://github.com/django/django/pull/14308

comment:4 by Zain Patel, 3 years ago

Has patch: set

comment:5 by Mariusz Felisiak, 3 years ago

Cc: Jon Dufresne added
Needs documentation: set
Needs tests: set
Owner: changed from nobody to Zain Patel
Severity: NormalRelease blocker
Status: newassigned
Summary: 'subtitle' missing from admin context on index pageVariable lookup errors are logged rendering the admin index page if subtitle is not defined.

Thanks for the report.

Regression in 84609b3205905097d7d3038d32e6101f012c0619.

comment:6 by Mariusz Felisiak, 3 years ago

Triage Stage: UnreviewedAccepted

comment:7 by Zain Patel, 3 years ago

Needs documentation: unset
Needs tests: unset

comment:8 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 4e5bbb6e:

Fixed #32681 -- Fixed VariableDoesNotExist when rendering some admin template.

Regression in 84609b3205905097d7d3038d32e6101f012c0619.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 0dfe88e:

[3.2.x] Fixed #32681 -- Fixed VariableDoesNotExist when rendering some admin template.

Regression in 84609b3205905097d7d3038d32e6101f012c0619.

Backport of 4e5bbb6ef2287126badd32842b239f4a8a7394ca from main.

in reply to:  description ; comment:11 by Sourav Kumar, 2 years ago

I'm still facing this same issue even after updating to the Django 3.2.9.

Replying to Zain Patel:

This looks like a bug introduced in Django 3.2 with the introduction of a new variable introduced in the base templates (django/contrib/admin/templates/base_site.html) named subtitle. This variable is passed in most places within the admin site, e.g in django/contrib/admin/options.py

Loading the admin index page with log-level debug shows the following exception/stack trace (that admittedly does not affect anything functional - simply clutters the logs):

Exception while resolving variable 'subtitle' in template 'admin/index.html'.
Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
  File "...lib/python3.7/site-packages/django/template/context.py", line 83, in __getitem__
    raise KeyError(key)
KeyError: 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 835, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'RequestContext' has no attribute 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 843, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...lib/python3.7/site-packages/django/template/base.py", line 850, in _resolve_lookup
    (bit, current))  # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [subtitle] in

I believe this can be fixed by providing subtitle: None in the AdminSite.each_context` method to default it to that. I am happy to create a PR for this.

On second though - instead of hardcoding subtitle to None - potentially it should be a customisable option in the AdminSite itself, so:

class MySite(AdminSite):
  site_header = "blah"
  subtitle = "my subtitle"

?

in reply to:  11 comment:12 by Mariusz Felisiak, 2 years ago

Replying to Sourav Kumar:

I'm still facing this same issue even after updating to the Django 3.2.9.

Thanks for the report. I prepared PR with follow up. Does it work for you?

comment:13 by Jon Dufresne, 2 years ago

Cc: Jon Dufresne removed

comment:14 by GitHub <noreply@…>, 2 years ago

In 0a4a5e5:

Refs #32681 -- Fixed VariableDoesNotExist when rendering some admin template.

Regression in 84609b3205905097d7d3038d32e6101f012c0619.

Follow up to 4e5bbb6ef2287126badd32842b239f4a8a7394ca.

Thanks Sourav Kumar for the report.

Note: See TracTickets for help on using tickets.
Back to Top