Opened 14 years ago
Closed 14 years ago
#13672 closed (duplicate)
template cache tag should gracefully handle uninitialized variables
Reported by: | nbv4 | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | dev |
Severity: | Keywords: | template cache | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I keep running into this problem. I have a page that uses the template cache tag. One of the variables that gets used as a cache key is sometimes available in the context, sometimes not (it's added to the context via middleware).
If the variable is not initialized, the whole page fails to render. EX:
context:
{key1: 'value', key2: 'another value'}
template:
{% if key3 %}
no errors are thrown here
{% endif %}
{% cache 1800 mycache key1 key2 key3 %}
causes a problem
{% endcache %}
result:
Caught VariableDoesNotExist while rendering: Failed lookup for key [key3] in u"[{'key1: 'value', key2: 'another value'}]"
I think it would be best to just have the key be "None" if it's uninitialized instead of throwing an error.
Attachments (1)
Change History (3)
by , 14 years ago
Attachment: | tcache.diff added |
---|
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
#13167 is open for tracking the general problem of VariableDoesNotExist being raised by non-existent filter/tag arguments. There is at least one tag (if) that suppresses this behavior, but in general VariableDoesNotExist has been raised for these error cases since before 1.0. The proposal to change the general behavior involves checking TEMPLATE_DEBUG to decide whether to raise an exception or not. Since I believe we want to fix the problem in general and not take a tag-by-tag approach, I'm going to close this as a dupe of that.
I don't know off hand which do and which don't but it seems fairly common for a tag to fail if the template doesn't exist. Presumably this could be resolved by wrapping {% cache %} in a if statement?
I think this might be a case of DDN. Since there is a fine line where failing gracefully is good and obfuscating errors is bad. I don't like the idea of thinking i'm caching only to realise later that the variable is not quite making it to the template.