jazzband / django-constance

Dynamic Django settings.
https://django-constance.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.72k stars 311 forks source link

'fieldsets' and 'subtitle' missing from Template context #586

Open spiceworm opened 1 month ago

spiceworm commented 1 month ago

Describe the problem

'fieldsets' and 'subtitle' are missing from template context which results in VariableDoesNotExist exceptions. fieldsets will only be missing if settings.CONFIG_FIELDSETS is falsy. I verified this is the problem by adding title=None, fieldsets=[] to context and it stop the exceptions from being thrown.

Steps to reproduce

Run django app with log level set to DEBUG and navigate to /admin/constance/config/

DEBUG 2024-09-14 02:57:31,496 base 471 128195625114496 Exception while resolving variable 'subtitle' in template 'admin/constance/change_list.html'.        
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/template/base.py", line 883, in _resolve_lookup
    current = current[bit]
              ~~~~~~~^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/template/context.py", line 85, in __getitem__
    raise KeyError(key)
KeyError: 'subtitle'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/template/base.py", line 889, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(
                                            ^^^^^^^^
AttributeError: type object 'RequestContext' has no attribute 'subtitle'
[2024-09-14 02:58:37 +0000] [500] [DEBUG] GET /admin/constance/config/                                                                                                                  
DEBUG 2024-09-14 02:58:37,761 base 500 140439392750464 Exception while resolving variable 'fieldsets' in template 'admin/constance/change_list.html'.
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/template/base.py", line 883, in _resolve_lookup
    current = current[bit]
              ~~~~~~~^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/template/context.py", line 85, in __getitem__
    raise KeyError(key)
KeyError: 'fieldsets'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/template/base.py", line 889, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(
                                            ^^^^^^^^
AttributeError: type object 'RequestContext' has no attribute 'fieldsets'

System configuration

Mogost commented 1 month ago

@spiceworm Do you use string_if_invalid? I believe it's why you are getting this exception. I'd recommend you not to use it. More details you can get in Adam's blogpost https://adamj.eu/tech/2022/03/30/how-to-make-django-error-for-undefined-template-variables/#with-the-string-if-invalid-option

spiceworm commented 1 month ago

I am not using string_if_invalid. See example in https://github.com/jazzband/django-constance/pull/587#issuecomment-2359180091