Last active
August 29, 2015 14:06
-
-
Save yuheiomori/0176424c949fa7d0afd2 to your computer and use it in GitHub Desktop.
errors_as_dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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