Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VariableDoesNotExist exceptions #587

Closed
wants to merge 1 commit into from

Conversation

spiceworm
Copy link

These were being thrown if fieldsets and subtitle
are missing from the context.

This commit fixes #586

These were being thrown if fieldsets and subtitle
are missing from the context.
Copy link
Member

@Mogost Mogost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django's template processor shouldn't raise an exception if a variable is missing.

@spiceworm
Copy link
Author

The exception does not halt execution, but it does appear in the DEBUG logs.

In [1]: from django.http import HttpRequest                                                                                                                       
   ...: from django.template import Template, RequestContext                                                                                                      
   ...:                                                                                                                                                           
   ...: request = HttpRequest()                                                                                                                                   
   ...: request.method = 'GET'                                                                                                                                    
   ...: request.META = {}                                                                                                                                            ...: request.META['SERVER_NAME'] = 'localhost'                                                                                                                 
                                        
In [2]: template = Template("This is a test {{ var_does_not_exist }}")
   ...: context = RequestContext(request)                                                                                                                         
   ...: template.render(context)
DEBUG 2024-09-18 18:46:20,401 base 216 139496148310912 Exception while resolving variable 'var_does_not_exist' in template 'unknown'.
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: 'var_does_not_exist'

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 'var_does_not_exist'

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 899, in _resolve_lookup
    current = current[int(bit)]
                      ^^^^^^^^
ValueError: invalid literal for int() with base 10: 'var_does_not_exist'

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 906, in _resolve_lookup
    raise VariableDoesNotExist(
django.template.base.VariableDoesNotExist: Failed lookup for key [var_does_not_exist] in [{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazy
Object: <function csrf.<locals>._get_val at 0x7edef47f9300>>, 'request': <HttpRequest>, 'user': <django.contrib.auth.models.AnonymousUser object at 0x7edef4307ce0
>, 'perms': PermWrapper(<django.contrib.auth.models.AnonymousUser object at 0x7edef4307ce0>), 'messages': [], 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 
'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}}, {}]
Out[2]: 'This is a test '

@Mogost
Copy link
Member

Mogost commented Sep 18, 2024

The behavior you're showing is typical for Django. If logs are too noisy, you can adjust the logging configuration. Missing context in Django templates is not an issue - {% if something %} works whether the variable exists or not, and both cases are handled safely.

For django-constance, tracking all possible context variables would add unnecessary maintenance overhead.

UPD:
If you insist on merging this PR, then a test is needed to confirm that the problem is fixed on all supported versions and there will be no undetectable regression in the future. But in general, this change seems unnecessary to me.

@camilonova
Copy link
Member

I'm closing this. I think it is not necessary.

@camilonova camilonova closed this Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'fieldsets' and 'subtitle' missing from Template context
3 participants