Django: Ticket #13672: template cache tag should gracefully handle uninitialized variableshttps://code.djangoproject.com/ticket/13672
<p>
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).
</p>
<p>
If the variable is not initialized, the whole page fails to render. EX:
</p>
<hr />
<p>
context:
</p>
<p>
{key1: 'value', key2: 'another value'}
</p>
<hr />
<p>
template:
</p>
<p>
{% if key3 %}
no errors are thrown here
{% endif %}
</p>
<p>
{% cache 1800 mycache key1 key2 key3 %}
causes a problem
{% endcache %}
</p>
<hr />
<p>
result:
</p>
<p>
Caught VariableDoesNotExist while rendering: Failed lookup for key [key3] in u"[{'key1: 'value', key2: 'another value'}]"
</p>
<p>
I think it would be best to just have the key be "None" if it's uninitialized instead of throwing an error.
</p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/13672
Trac 1.2nbv4Mon, 31 May 2010 20:38:47 GMTattachment sethttps://code.djangoproject.com/ticket/13672
https://code.djangoproject.com/ticket/13672
<ul>
<li><strong>attachment</strong>
set to <em>tcache.diff</em>
</li>
</ul>
TicketDougal MatthewsTue, 01 Jun 2010 07:05:48 GMThttps://code.djangoproject.com/ticket/13672#comment:1
https://code.djangoproject.com/ticket/13672#comment:1
<p>
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?
</p>
<p>
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.
</p>
TicketKaren TraceyTue, 01 Jun 2010 10:20:01 GMTstatus changed; resolution sethttps://code.djangoproject.com/ticket/13672#comment:2
https://code.djangoproject.com/ticket/13672#comment:2
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>duplicate</em>
</li>
</ul>
<p>
<a class="closed ticket" href="https://code.djangoproject.com/ticket/13167" title="#13167: Bug: Non-existent arg passed to template filter raises VariableDoesNotExist (closed: wontfix)">#13167</a> 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.
</p>
Ticket