django-pipeline
django-pipeline copied to clipboard
Upgraded Django 1.8 to 1.11, getting lots of VariableDoesNotExist errors
The keys it's trying to look for include:
- in
pipeline/js.html
:-
async
-
defer
-
- in
pipeline/css.html
:-
title
-
charset
-
The keys available are:
-
url
-
media
(only available inpipeline/css.html
-
type
Do you have a solid example repo that can be used for verifying this ?
Not public, no, sorry. But using the PyCharm debugger I did trace it to these templates and variables.
The best I could tell is that Django is simply being more verbose with the template errors and warnings, but the behavior stays the same that the {% if variable %}
tag can be safely used to check existence, and a non-existing variable will resolve to False
and the template will continue rendering, because it didn't actually cause any problems for us.
I believe this issue can be closed as it's not a bug, just an indication of verbose logging.
The most prominent place where this always appears in pipeline is pipeline/css.html
All 3 variables media
, title
and charset
don't have default values and raise a KeyError
in the renderer. Adding a default value in pipeline's render context will resolve this, and most probably reduce some noise (which could result in a slight performance boost).
I can submit a PR if adding defaults sounds reasonable, but not sure what I would go with, probably either empty string or None
, what do you say @hsiaoyi0504 ?
05-06-2019 06:13:13 DEBUG MainProcess 64398 django.template [base.py:878] Exception while resolving variable 'media' in template 'pipeline/css.html'.
Traceback (most recent call last):
File "/Users/pavel.savchenko/.virtualenvs/test/lib/python3.6/site-packages/django/template/base.py", line 835, in _resolve_lookup
current = current[bit]
File "/Users/pavel.savchenko/.virtualenvs/test/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'media'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/pavel.savchenko/.virtualenvs/test/lib/python3.6/site-packages/django/template/base.py", line 841, in _resolve_lookup
if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'Context' has no attribute 'media'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/pavel.savchenko/.virtualenvs/test/lib/python3.6/site-packages/django/template/base.py", line 849, in _resolve_lookup
current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'media'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/pavel.savchenko/.virtualenvs/test/lib/python3.6/site-packages/django/template/base.py", line 856, in _resolve_lookup
(bit, current)) # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [media] in [{'True': True, 'False': False, 'None': None}, {'type': 'text/css', 'url': '/static/ui/assets/styles/styles.css'}]
For what it's worth, I'm also experiencing this error in the course of upgrading from Django 1.8.4 to 1.11.21. I'm getting it for the variables "media," "title," and "charset" in css.html and "async" and "defer" in js.html. Is there anything I can do in terms of configuring django-pipeline or my Django settings or static files to prevent these errors from occurring or being logged until a fix is found? There are so many of them in my logs that it's difficult to see when real errors are occurring. Thank you.
Suddenly having the same issue, not sure what we've updated recently - currently checking. But we are on Django 2.0 and tried using both django-pipeline 1.6.14 and 1.7.0
Solution for us was to create default "extra content" objects and add it to all of our stylesheets and scripts, e.g.:
default_stylesheet_extra_content = {
'media': 'all',
'charset': None,
'title': None,
}
PIPELINE['STYLESHEETS'] = {
'countdown': {
'source_filenames': (
'css/jquery.countdown-v1.6.0.css',
'sass/countdown.scss',
),
'output_filename': 'css/countdown.css',
'extra_context': default_stylesheet_extra_content,
},
...
}
default_js_extra_content = {
'async': False,
'defer': False,
}
PIPELINE['JAVASCRIPT'] = {
'feeds': {
'source_filenames': (
'js/feeds.js',
),
'output_filename': 'js/feeds.js',
'extra_context': default_js_extra_content,
},
...
}
@asfaltboy's suggested solution would've been great though.