Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a bit more developer friendly exception message #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion formulation/templatetags/formulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is incorrect.

This case is when you try to indirectly reference a field [using a string] but haven't specified a form in the form tag.

'exist on the form.'
)
else:
field = formulation_form[field]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A KeyError here is where the above error would make sense.


field_data = {
'form_field': field,
Expand Down
14 changes: 14 additions & 0 deletions tests/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}",
Expand All @@ -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.
Expand Down