diff --git a/formulation/templatetags/formulation.py b/formulation/templatetags/formulation.py index 06bf728..3d81f6f 100644 --- a/formulation/templatetags/formulation.py +++ b/formulation/templatetags/formulation.py @@ -118,7 +118,14 @@ def render(self, context): @register.simple_tag(takes_context=True) def field(context, field, widget=None, **kwargs): if isinstance(field, six.string_types): - field = context['formulation-form'][field] + formulation_form = context['formulation-form'] + if formulation_form is None: + raise ValueError( + 'The form field you\'re trying to access probably doesn\'t ' + 'exist on the form.' + ) + else: + field = formulation_form[field] field_data = { 'form_field': field, diff --git a/tests/tests/test_tags.py b/tests/tests/test_tags.py index e533465..82fbfce 100644 --- a/tests/tests/test_tags.py +++ b/tests/tests/test_tags.py @@ -99,6 +99,7 @@ class FieldTagTest(TemplateTestMixin, SimpleTestCase): PARTIALS = { 'use_correct_block': "{% field form.name 'custom_input' %}", 'unknown_block': "{% field form.name 'does_not_exist' %}", + 'unknown_field': "{% field form.choose_unicorn 'TextInput' %}", 'auto_widget1': "{% field form.name %}", 'auto_widget2': "{% field form.gender %}", 'auto_widget3': "{% field form.is_cool %}", @@ -124,6 +125,19 @@ def test_unknown_block(self): with self.assertRaises(TemplateSyntaxError): template.render(self.context) + def test_wrong_field(self): + """ + Tests the debug information when trying to render a non-existent field. + """ + template = get_template('unknown_field') + with self.assertRaises(ValueError) as exc: + template.render(self.context) + self.assertEqual( + exc.exception.args[0], + 'The form field you\'re trying to access probably doesn\'t ' + 'exist on the form.' + ) + def test_auto_widget(self): """ Choose the correct widget according to the form field.