Skip to content

Instantly share code, notes, and snippets.

@yuheiomori
Last active August 29, 2015 14:06
Show Gist options
  • Save yuheiomori/0176424c949fa7d0afd2 to your computer and use it in GitHub Desktop.
Save yuheiomori/0176424c949fa7d0afd2 to your computer and use it in GitHub Desktop.
errors_as_dict
# coding=utf-8
from django import forms
from django.template.defaultfilters import striptags
class PersonForm(forms.Form):
name = forms.CharField()
age = forms.IntegerField()
def errors_as_dict(self):
errors = {}
for error in self.errors.iteritems():
errors.update({error[0]: unicode(striptags(error[1]))})
return errors
from django.test import TestCase
from . import forms
class PersonFormTest(TestCase):
def test_errors_as_dict(self):
f = forms.PersonForm({})
self.assertFalse(f.is_valid())
self.assertDictEqual(f.errors_as_dict(),
{'age': u'This field is required.',
'name': u'This field is required.'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment