This test:
from django.template import Template, Context
from django.test import TestCase
class TemplateTestCase(TestCase):
def test_template(self):
t = Template('''
{% for p in parts %}
{{ p.name }} is {{ p.color }} and {% if p.clear %}clear{% else %}not clear{% endif %}.
{% endfor %}
''')
context = {
'parts': [
{
'name': 'one',
'color': 'red',
},
{
'name': 'two',
'color': 'green',
'clear': False
},
{
'name': 'three',
'color': 'blue',
}
]
}
s1 = t.render(Context(context)).strip()
s2 = t.render(Context(context)).strip()
self.assertEqual(s1, s2)
fails, perhaps unexpectedly. The problem is caused by the key clear
not being present in the passed context in some of the dictionaries, with the {% if p.clear %}
clearing the dictionary represented by p
because its dict.clear()
method is called. I know the ability to access a no-argument method result is useful in templates, but is it documented that certain keys, like clear
, are a no-go-area? Or is this a bug which requires extra checks in Django to avoid calling dict.clear()
, list.clear()
and the like?