#12554: silent_variable_failure not suppressing exception during dict lookup
---------------------------------------------------------+------------------
Reporter: Thomas Steinacher <[email protected]> | Owner:
nobody
Status: reopened | Milestone: 1.2
Component: Template system | Version: SVN
Resolution: | Keywords:
Stage: Accepted | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 1 |
---------------------------------------------------------+------------------
Changes (by anonymous):
* status: closed => reopened
* resolution: fixed =>
Comment:
When I do this:
{{{
from django.template import Context, Template
from myapp.models import MyModel
MyModel(models.Model):
some = model.OneToOneField(AnotherModel)
obj = MyModel.objects.get(id=1)
t = Template("{{ obj.some }}")
t.render(Context({"obj": obj}))
}}}
Django raises the exception:
{{{
---------------------------------------------------------------------------
TemplateSyntaxError Traceback (most recent call
last)
/home/alejandro/inmegen/<ipython console> in <module>()
/usr/lib/python2.6/site-packages/django/template/__init__.pyc in
render(self, context)
169 context.render_context.push()
170 try:
--> 171 return self._render(context)
172 finally:
173 context.render_context.pop()
/usr/lib/python2.6/site-packages/django/template/__init__.pyc in
_render(self, context)
163
164 def _render(self, context):
--> 165 return self.nodelist.render(context)
166
167 def render(self, context):
/usr/lib/python2.6/site-packages/django/template/__init__.pyc in
render(self, context)
795 for node in self:
796 if isinstance(node, Node):
--> 797 bits.append(self.render_node(node, context))
798 else:
799 bits.append(node)
TemplateSyntaxError: Caught DoesNotExist while rendering: AnotherModel
matching query does not exist.
}}}
Proposed patch:
{{{
Index: django/template/__init__.py
===================================================================
--- django/template/__init__.py (revision 12823)
+++ django/template/__init__.py (working copy)
@@ -745,6 +745,11 @@
TypeError, # unsubscriptable object
):
raise VariableDoesNotExist("Failed lookup for key
[%s] in %r", (bit, current)) # missing attribute
+ except Exception, e:
+ if getattr(e, 'silent_variable_failure', False):
+ current = settings.TEMPLATE_STRING_IF_INVALID
+ else:
+ raise
except Exception, e:
if getattr(e, 'silent_variable_failure', False):
current = settings.TEMPLATE_STRING_IF_INVALID
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/12554#comment:10>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.