Opened 9 years ago
Closed 6 weeks ago
#26886 closed Cleanup/optimization (duplicate)
TECHNICAL_404_TEMPLATE logs an "Exception while resolving variable" warning for URLResolver in urlpatterns
| Reported by: | None | Owned by: | Jacob Walls |
|---|---|---|---|
| Component: | Error reporting | Version: | 1.9 |
| Severity: | Normal | Keywords: | |
| Cc: | tabbott@… | Triage Stage: | Unreviewed |
| Has patch: | yes | 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 (11)
comment:1 by , 9 years ago
| Resolution: | → needsinfo |
|---|---|
| Status: | new → closed |
comment:2 by , 9 years ago
| Resolution: | needsinfo |
|---|---|
| Status: | closed → new |
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 , 9 years ago
| Component: | Template system → Error 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 , 9 years ago
| Summary: | Wrong warnings during template rendering → TECHNICAL_404_TEMPLATE logs an "Exception while resolving variable" warning |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
Accepting for further thought.
comment:5 by , 9 years ago
| Cc: | 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 , 9 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 , 8 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
comment:8 by , 8 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
I created #28526 to address the problem of verbose, often unhelpful logging of undefined variables.
comment:9 by , 7 weeks ago
| Has patch: | set |
|---|---|
| Resolution: | duplicate |
| Status: | closed → new |
| Summary: | TECHNICAL_404_TEMPLATE logs an "Exception while resolving variable" warning → TECHNICAL_404_TEMPLATE logs an "Exception while resolving variable" warning for URLResolver in urlpatterns |
| Triage Stage: | Accepted → Unreviewed |
Ran into the giant traceback from comment:7 running a Django app where my only urlpattern is path("admin/", admin.site.urls) and my browser requested /favicon.ico.
Regardless of #28526, I think there is a simple solution along the lines of comment:3 to improve this specific template, see PR. I think this is worth improving to improve the DX in the early days of working on a Django site.
comment:10 by , 7 weeks ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:11 by , 6 weeks ago
| Resolution: | → duplicate |
|---|---|
| Status: | assigned → closed |
I think we can still keep the decision to track in #28526, as we can have Refs #28526 and I don't believe having a wider ticket means that incremental fixes are not wanted
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.