Opened 8 years ago

Closed 7 years ago

#26886 closed Cleanup/optimization (duplicate)

TECHNICAL_404_TEMPLATE logs an "Exception while resolving variable" warning

Reported by: None Owned by: None
Component: Error reporting Version: 1.9
Severity: Normal Keywords:
Cc: tabbott@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There is template:

    <div style="{{ style }}"></div>

and code:

    render(request, 'template.html', context={'style': "background-image: url('image.png');"})

After run it I get some strange console output:

    Exception while resolving variable 'name' in template 'unknown'.
    Traceback (most recent call last):
    File ".../.virtualenvs/project/local/lib/python2.7/site-packages/django/template/base.py", line 905, in _resolve_lookup
    (bit, current))  # missing attribute
    VariableDoesNotExist: Failed lookup for key [name] in u'<RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>'
    Exception while resolving variable 'name' in template 'unknown'.
    Traceback (most recent call last):
    File ".../.virtualenvs/project/local/lib/python2.7/site-packages/django/template/base.py", line 905, in _resolve_lookup
    (bit, current))  # missing attribute
    VariableDoesNotExist: Failed lookup for key [name] in u"<RegexURLResolver <module 'django.contrib.admindocs.urls' from '.../.virtualenvs/project/local/lib/python2.7/site-packages/django/contrib/admindocs/urls.pyc'> (None:None) ^admin/doc/>"
    ....
    etc.

Change History (8)

comment:1 by Tim Graham, 8 years ago

Resolution: needsinfo
Status: newclosed

Could you please provide a sample project to reproduce the issue? That minimal view and template doesn't create that error if I add it to the tutorial's project so we're probably missing some steps to reproduce.

comment:2 by nrogers64, 8 years ago

Resolution: needsinfo
Status: closednew

I wasn't the one to report this bug, but I ran into the same thing. I made a fresh Django 1.10.1 project and added this to settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.template': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

That is the only code I added. I then did the initial migrate and started runserver. Then, in another Terminal tab, I ran curl http://localhost:8001/favicon.ico, which resulted in a "Page not found at /favicon.ico", as expected. But this was in the output of runserver:

Exception while resolving variable 'name' in template 'unknown'.
Traceback (most recent call last):
  File ".../.virtualenvs/project/local/lib/python2.7/site-packages/django/template/base.py", line 907, in _resolve_lookup
    (bit, current))  # missing attribute
VariableDoesNotExist: Failed lookup for key [name] in u'<RegexURLResolver <RegexURLPattern list> (admin:admin) ^admin/>'

comment:3 by Tim Graham, 8 years ago

Component: Template systemError reporting

The error comes from the TECHNICAL_404_TEMPLATE: {% if forloop.last and pat.name %}[name='{{ pat.name }}']{% endif %}

There isn't a problem besides the confusing logging. The error occurs because RegexURLResolver doesn't have a name attribute so the exception is expected. Only a RegexURLPattern has a name in that loop. I'm not sure if there's a simple solution to avoid that exception logging (added in #18773).

comment:4 by Tim Graham, 8 years ago

Summary: Wrong warnings during template renderingTECHNICAL_404_TEMPLATE logs an "Exception while resolving variable" warning
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Accepting for further thought.

comment:5 by Tim Abbott, 8 years ago

Cc: tabbott@… added

We were seeing this a lot in the Zulip development environment and found it pretty confusing for new contributors, so we worked around it using https://github.com/zulip/zulip/pull/4076.

comment:6 by Tim Graham, 8 years ago

I started a thread on the django-developers mailing list to see if there might be consensus to remove logging of undefined variables.

comment:7 by Sharmila G Sivakumar, 7 years ago

Hi,

I another developer who has encountered the same issue. I perused the comments on the mailing list also to see how this would be handled. I agree with the sentiment that errors need to be raised as they are good indicators of underlying issues that need to be fixed. That is true even if, overall, the system works. On the other hand it would be great if the error can be identified for what it is - An known error raised by django internals and handled accordingly. Like the contributer Antony King suggested, a single line capturing the essence of the error would suffice


some_app/home.html:32:24: Undefined variable: missing_variable

The reason I am suggesting this is, I am using Django 1.11 and the Template_404's error messages are drowning out all other errors. The error is basically caused by favicon.ico resulting in a 404. In the process of development, favicons are the last priority. So, the developers might have to endure the error messages for a long time. Adding a temporary favicon is a possibility, but I would rather prefer a "GET /favicon.ico HTTP/1.1" 404 4296 message on the console to remind that favicon is not set :)

I am providing an example of an exception trace from a single favicon 404 error. May be I am doing something wrong too. If so please suggest an appropriate correction.

Exception while resolving variable 'name' in template 'unknown'.
Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner

response = get_response(request)

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 172, in _get_response

resolver_match = resolver.resolve(request.path_info)

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 392, in resolve

raise Resolver404({'tried': tried, 'path': new_path})

django.urls.exceptions.Resolver404: {'tried': [[<RegexURLResolver <module 'debug_toolbar.toolbar' from '/home/code/product_recco/reddit_env/lib/python3.6/site-packages/debug_toolbar/toolbar.py'> (djdt:djdt) debug/>], [<RegexURLResolver <RegexURLPattern list> (admin:admin) admin/>], [<RegexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssay/urls.py'> (None:None) >, <RegexURLPattern index $>], [<RegexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssay/urls.py'> (None:None) >, <RegexURLPattern subscribe subscribe$>], [<Re
gexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssay/
urls.py'> (None:None) >, <RegexURLPattern search search$>], [<RegexURLResolver <module 'userssay.urls' f
rom '/home/code/product_recco/camerasforpros/src/recoapp/userssay/urls.py'> (None:None) >, <RegexURLPatte
rn category_most_voted
categories/(?P<cat_slug>[a-zA-Z0-9-]+)/most-voted/$>], [<RegexURLResolver <module
'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssay/urls.py'> (None:None) >, <RegexURLPattern category_most_recent categories/(?P<cat_slug>[a-zA-Z0-9-]+)/most-recent/$>], [<RegexU
RLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssay/urls
.py'> (None:None) >, <RegexURLPattern tag_most_voted tags/(?P<tag_slug>[a-zA-Z0-9-]+)/most-voted/$>], [<
RegexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssa
y/urls.py'> (None:None) >, <RegexURLPattern tag_most_recent tags/(?P<tag_slug>[a-zA-Z0-9-]+)/most-recent
/$>], [<RegexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp
/userssay/urls.py'> (None:None) >, <RegexURLPattern get_product_info product/(?P<id_>[0-9]+)/(?P<slug>[a
-zA-Z0-9-_]+)/$>], [<RegexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpros/src/recoapp/userssay/urls.py'> (None:None) >, <RegexURLPattern get_accessories accessories/(?P<slug>[a
-zA-Z0-9-_]+)/$>], [<RegexURLResolver <module 'userssay.urls' from '/home/code/product_recco/camerasforpro
s/src/recoapp/userssay/urls.py'> (None:None) >, <RegexURLPattern django.contrib.sitemaps.views.sitemap s
itemap\.xml$>], [<RegexURLPattern None static\/(?P<path>.*)$>]], 'path': 'favicon.ico'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 882

, in _resolve_lookup

current = current[bit]

TypeError: 'RegexURLResolver' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 890

, in _resolve_lookup

current = getattr(current, bit)

AttributeError: 'RegexURLResolver' object has no attribute 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 896, in _resolve_lookup

current = current[int(bit)]

ValueError: invalid literal for int() with base 10: 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 903, in _resolve_lookup

(bit, current)) # missing attribute

django.template.base.VariableDoesNotExist: Failed lookup for key [name] in "<RegexURLResolver <module 'debug_toolbar.toolbar' from '/home/code/product_recco/reddit_env/lib/python3.6/site-packages/debug_toolbar/toolbar.py'> (djdt:djdt) debug/>"
Exception while resolving variable 'name' in template 'unknown'.
Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner

response = get_response(request)

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 172, in _get_response

resolver_match = resolver.resolve(request.path_info)

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 392, in resolve

raise Resolver404({'tried': tried, 'path': new_path})

django.urls.exceptions.Resolver404: {'tried': [.....................huge..list..of..urls..removed.........................], 'path': 'favicon.ico'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 882, in _resolve_lookup

current = current[bit]

TypeError: 'RegexURLResolver' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 890, in _resolve_lookup

current = getattr(current, bit)

AttributeError: 'RegexURLResolver' object has no attribute 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 896, in _resolve_lookup

current = current[int(bit)]

ValueError: invalid literal for int() with base 10: 'name'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/home/code/product_recco/reddit_env/lib/python3.6/site-packages/django/template/base.py", line 903, in _resolve_lookup

(bit, current)) # missing attribute

django.template.base.VariableDoesNotExist: Failed lookup for key [name] in '<RegexURLResolver <RegexURLPattern list> (admin:admin) admin/>'
Not Found: /favicon.ico
"GET /favicon.ico HTTP/1.1" 404 4296

Version 0, edited 7 years ago by Sharmila G Sivakumar (next)

comment:8 by Tim Graham, 7 years ago

Resolution: duplicate
Status: newclosed

I created #28526 to address the problem of verbose, often unhelpful logging of undefined variables.

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