Ticket #3395: unicode_newforms_errors_new.patch

File unicode_newforms_errors_new.patch, 28.6 KB (added by Honza Král <Honza.Kral@…>, 18 years ago)

this one actually works ;)

  • django/newforms/util.py

     
    55# a leading space. Assumes keys do not need to be XML-escaped.
    66flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
    77
     8def smart_deunicode(s):
     9    if not isinstance(s, basestring):
     10        s = str(s)
     11    elif not isinstance(s, str):
     12        s = s.encode(settings.DEFAULT_CHARSET)
     13    return s
     14
    815def smart_unicode(s):
    916    if not isinstance(s, basestring):
    1017        s = unicode(str(s))
     
    5764    def __init__(self, message):
    5865        "ValidationError can be passed a string or a list."
    5966        if isinstance(message, list):
    60             self.messages = ErrorList([smart_unicode(msg) for msg in message])
     67            self.messages = ErrorList([smart_deunicode(msg) for msg in message])
    6168        else:
    6269            assert isinstance(message, basestring), ("%s should be a basestring" % repr(message))
    63             message = smart_unicode(message)
     70            message = smart_deunicode(message)
    6471            self.messages = ErrorList([message])
    6572
    6673    def __str__(self):
  • tests/modeltests/model_forms/models.py

     
    112112If you call save() with invalid data, you'll get a ValueError.
    113113>>> f = CategoryForm({'name': '', 'url': 'foo'})
    114114>>> f.errors
    115 {'name': [u'This field is required.']}
     115{'name': ['This field is required.']}
    116116>>> f.clean_data
    117117Traceback (most recent call last):
    118118...
  • tests/regressiontests/forms/tests.py

     
    805805>>> f.clean(None)
    806806Traceback (most recent call last):
    807807...
    808 ValidationError: [u'This field is required.']
     808ValidationError: ['This field is required.']
    809809>>> f.clean('')
    810810Traceback (most recent call last):
    811811...
    812 ValidationError: [u'This field is required.']
     812ValidationError: ['This field is required.']
    813813>>> f.clean([1, 2, 3])
    814814u'[1, 2, 3]'
    815815
     
    834834>>> f.clean('1234567890a')
    835835Traceback (most recent call last):
    836836...
    837 ValidationError: [u'Ensure this value has at most 10 characters.']
     837ValidationError: ['Ensure this value has at most 10 characters.']
    838838
    839839CharField accepts an optional min_length parameter:
    840840>>> f = CharField(min_length=10, required=False)
     
    843843>>> f.clean('12345')
    844844Traceback (most recent call last):
    845845...
    846 ValidationError: [u'Ensure this value has at least 10 characters.']
     846ValidationError: ['Ensure this value has at least 10 characters.']
    847847>>> f.clean('1234567890')
    848848u'1234567890'
    849849>>> f.clean('1234567890a')
     
    853853>>> f.clean('')
    854854Traceback (most recent call last):
    855855...
    856 ValidationError: [u'This field is required.']
     856ValidationError: ['This field is required.']
    857857>>> f.clean('12345')
    858858Traceback (most recent call last):
    859859...
    860 ValidationError: [u'Ensure this value has at least 10 characters.']
     860ValidationError: ['Ensure this value has at least 10 characters.']
    861861>>> f.clean('1234567890')
    862862u'1234567890'
    863863>>> f.clean('1234567890a')
     
    869869>>> f.clean('')
    870870Traceback (most recent call last):
    871871...
    872 ValidationError: [u'This field is required.']
     872ValidationError: ['This field is required.']
    873873>>> f.clean(None)
    874874Traceback (most recent call last):
    875875...
    876 ValidationError: [u'This field is required.']
     876ValidationError: ['This field is required.']
    877877>>> f.clean('1')
    8788781
    879879>>> isinstance(f.clean('1'), int)
     
    883883>>> f.clean('a')
    884884Traceback (most recent call last):
    885885...
    886 ValidationError: [u'Enter a whole number.']
     886ValidationError: ['Enter a whole number.']
    887887>>> f.clean('1 ')
    8888881
    889889>>> f.clean(' 1')
     
    893893>>> f.clean('1a')
    894894Traceback (most recent call last):
    895895...
    896 ValidationError: [u'Enter a whole number.']
     896ValidationError: ['Enter a whole number.']
    897897
    898898>>> f = IntegerField(required=False)
    899899>>> f.clean('')
     
    911911>>> f.clean('a')
    912912Traceback (most recent call last):
    913913...
    914 ValidationError: [u'Enter a whole number.']
     914ValidationError: ['Enter a whole number.']
    915915>>> f.clean('1 ')
    9169161
    917917>>> f.clean(' 1')
     
    921921>>> f.clean('1a')
    922922Traceback (most recent call last):
    923923...
    924 ValidationError: [u'Enter a whole number.']
     924ValidationError: ['Enter a whole number.']
    925925
    926926IntegerField accepts an optional max_value parameter:
    927927>>> f = IntegerField(max_value=10)
    928928>>> f.clean(None)
    929929Traceback (most recent call last):
    930930...
    931 ValidationError: [u'This field is required.']
     931ValidationError: ['This field is required.']
    932932>>> f.clean(1)
    9339331
    934934>>> f.clean(10)
     
    936936>>> f.clean(11)
    937937Traceback (most recent call last):
    938938...
    939 ValidationError: [u'Ensure this value is less than or equal to 10.']
     939ValidationError: ['Ensure this value is less than or equal to 10.']
    940940>>> f.clean('10')
    94194110
    942942>>> f.clean('11')
    943943Traceback (most recent call last):
    944944...
    945 ValidationError: [u'Ensure this value is less than or equal to 10.']
     945ValidationError: ['Ensure this value is less than or equal to 10.']
    946946
    947947IntegerField accepts an optional min_value parameter:
    948948>>> f = IntegerField(min_value=10)
    949949>>> f.clean(None)
    950950Traceback (most recent call last):
    951951...
    952 ValidationError: [u'This field is required.']
     952ValidationError: ['This field is required.']
    953953>>> f.clean(1)
    954954Traceback (most recent call last):
    955955...
    956 ValidationError: [u'Ensure this value is greater than or equal to 10.']
     956ValidationError: ['Ensure this value is greater than or equal to 10.']
    957957>>> f.clean(10)
    95895810
    959959>>> f.clean(11)
     
    968968>>> f.clean(None)
    969969Traceback (most recent call last):
    970970...
    971 ValidationError: [u'This field is required.']
     971ValidationError: ['This field is required.']
    972972>>> f.clean(1)
    973973Traceback (most recent call last):
    974974...
    975 ValidationError: [u'Ensure this value is greater than or equal to 10.']
     975ValidationError: ['Ensure this value is greater than or equal to 10.']
    976976>>> f.clean(10)
    97797710
    978978>>> f.clean(11)
     
    986986>>> f.clean(21)
    987987Traceback (most recent call last):
    988988...
    989 ValidationError: [u'Ensure this value is less than or equal to 20.']
     989ValidationError: ['Ensure this value is less than or equal to 20.']
    990990
    991991# DateField ###################################################################
    992992
     
    10191019>>> f.clean('2006-4-31')
    10201020Traceback (most recent call last):
    10211021...
    1022 ValidationError: [u'Enter a valid date.']
     1022ValidationError: ['Enter a valid date.']
    10231023>>> f.clean('200a-10-25')
    10241024Traceback (most recent call last):
    10251025...
    1026 ValidationError: [u'Enter a valid date.']
     1026ValidationError: ['Enter a valid date.']
    10271027>>> f.clean('25/10/06')
    10281028Traceback (most recent call last):
    10291029...
    1030 ValidationError: [u'Enter a valid date.']
     1030ValidationError: ['Enter a valid date.']
    10311031>>> f.clean(None)
    10321032Traceback (most recent call last):
    10331033...
    1034 ValidationError: [u'This field is required.']
     1034ValidationError: ['This field is required.']
    10351035
    10361036>>> f = DateField(required=False)
    10371037>>> f.clean(None)
     
    10551055>>> f.clean('2006-10-25')
    10561056Traceback (most recent call last):
    10571057...
    1058 ValidationError: [u'Enter a valid date.']
     1058ValidationError: ['Enter a valid date.']
    10591059>>> f.clean('10/25/2006')
    10601060Traceback (most recent call last):
    10611061...
    1062 ValidationError: [u'Enter a valid date.']
     1062ValidationError: ['Enter a valid date.']
    10631063>>> f.clean('10/25/06')
    10641064Traceback (most recent call last):
    10651065...
    1066 ValidationError: [u'Enter a valid date.']
     1066ValidationError: ['Enter a valid date.']
    10671067
    10681068# TimeField ###################################################################
    10691069
     
    10801080>>> f.clean('hello')
    10811081Traceback (most recent call last):
    10821082...
    1083 ValidationError: [u'Enter a valid time.']
     1083ValidationError: ['Enter a valid time.']
    10841084>>> f.clean('1:24 p.m.')
    10851085Traceback (most recent call last):
    10861086...
    1087 ValidationError: [u'Enter a valid time.']
     1087ValidationError: ['Enter a valid time.']
    10881088
    10891089TimeField accepts an optional input_formats parameter:
    10901090>>> f = TimeField(input_formats=['%I:%M %p'])
     
    11021102>>> f.clean('14:30:45')
    11031103Traceback (most recent call last):
    11041104...
    1105 ValidationError: [u'Enter a valid time.']
     1105ValidationError: ['Enter a valid time.']
    11061106
    11071107# DateTimeField ###############################################################
    11081108
     
    11431143>>> f.clean('hello')
    11441144Traceback (most recent call last):
    11451145...
    1146 ValidationError: [u'Enter a valid date/time.']
     1146ValidationError: ['Enter a valid date/time.']
    11471147>>> f.clean('2006-10-25 4:30 p.m.')
    11481148Traceback (most recent call last):
    11491149...
    1150 ValidationError: [u'Enter a valid date/time.']
     1150ValidationError: ['Enter a valid date/time.']
    11511151
    11521152DateField accepts an optional input_formats parameter:
    11531153>>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p'])
     
    11671167>>> f.clean('2006-10-25 14:30:45')
    11681168Traceback (most recent call last):
    11691169...
    1170 ValidationError: [u'Enter a valid date/time.']
     1170ValidationError: ['Enter a valid date/time.']
    11711171
    11721172>>> f = DateTimeField(required=False)
    11731173>>> f.clean(None)
     
    11871187>>> f.clean('3G3')
    11881188Traceback (most recent call last):
    11891189...
    1190 ValidationError: [u'Enter a valid value.']
     1190ValidationError: ['Enter a valid value.']
    11911191>>> f.clean(' 2A2')
    11921192Traceback (most recent call last):
    11931193...
    1194 ValidationError: [u'Enter a valid value.']
     1194ValidationError: ['Enter a valid value.']
    11951195>>> f.clean('2A2 ')
    11961196Traceback (most recent call last):
    11971197...
    1198 ValidationError: [u'Enter a valid value.']
     1198ValidationError: ['Enter a valid value.']
    11991199>>> f.clean('')
    12001200Traceback (most recent call last):
    12011201...
    1202 ValidationError: [u'This field is required.']
     1202ValidationError: ['This field is required.']
    12031203
    12041204>>> f = RegexField('^\d[A-F]\d$', required=False)
    12051205>>> f.clean('2A2')
     
    12091209>>> f.clean('3G3')
    12101210Traceback (most recent call last):
    12111211...
    1212 ValidationError: [u'Enter a valid value.']
     1212ValidationError: ['Enter a valid value.']
    12131213>>> f.clean('')
    12141214u''
    12151215
     
    12221222>>> f.clean('3G3')
    12231223Traceback (most recent call last):
    12241224...
    1225 ValidationError: [u'Enter a valid value.']
     1225ValidationError: ['Enter a valid value.']
    12261226>>> f.clean(' 2A2')
    12271227Traceback (most recent call last):
    12281228...
    1229 ValidationError: [u'Enter a valid value.']
     1229ValidationError: ['Enter a valid value.']
    12301230>>> f.clean('2A2 ')
    12311231Traceback (most recent call last):
    12321232...
    1233 ValidationError: [u'Enter a valid value.']
     1233ValidationError: ['Enter a valid value.']
    12341234
    12351235RegexField takes an optional error_message argument:
    12361236>>> f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.')
     
    12391239>>> f.clean('123')
    12401240Traceback (most recent call last):
    12411241...
    1242 ValidationError: [u'Enter a four-digit number.']
     1242ValidationError: ['Enter a four-digit number.']
    12431243>>> f.clean('abcd')
    12441244Traceback (most recent call last):
    12451245...
    1246 ValidationError: [u'Enter a four-digit number.']
     1246ValidationError: ['Enter a four-digit number.']
    12471247
    12481248RegexField also access min_length and max_length parameters, for convenience.
    12491249>>> f = RegexField('^\d+$', min_length=5, max_length=10)
    12501250>>> f.clean('123')
    12511251Traceback (most recent call last):
    12521252...
    1253 ValidationError: [u'Ensure this value has at least 5 characters.']
     1253ValidationError: ['Ensure this value has at least 5 characters.']
    12541254>>> f.clean('abc')
    12551255Traceback (most recent call last):
    12561256...
    1257 ValidationError: [u'Ensure this value has at least 5 characters.']
     1257ValidationError: ['Ensure this value has at least 5 characters.']
    12581258>>> f.clean('12345')
    12591259u'12345'
    12601260>>> f.clean('1234567890')
     
    12621262>>> f.clean('12345678901')
    12631263Traceback (most recent call last):
    12641264...
    1265 ValidationError: [u'Ensure this value has at most 10 characters.']
     1265ValidationError: ['Ensure this value has at most 10 characters.']
    12661266>>> f.clean('12345a')
    12671267Traceback (most recent call last):
    12681268...
    1269 ValidationError: [u'Enter a valid value.']
     1269ValidationError: ['Enter a valid value.']
    12701270
    12711271# EmailField ##################################################################
    12721272
     
    12741274>>> f.clean('')
    12751275Traceback (most recent call last):
    12761276...
    1277 ValidationError: [u'This field is required.']
     1277ValidationError: ['This field is required.']
    12781278>>> f.clean(None)
    12791279Traceback (most recent call last):
    12801280...
    1281 ValidationError: [u'This field is required.']
     1281ValidationError: ['This field is required.']
    12821282>>> f.clean('person@example.com')
    12831283u'person@example.com'
    12841284>>> f.clean('foo')
    12851285Traceback (most recent call last):
    12861286...
    1287 ValidationError: [u'Enter a valid e-mail address.']
     1287ValidationError: ['Enter a valid e-mail address.']
    12881288>>> f.clean('foo@')
    12891289Traceback (most recent call last):
    12901290...
    1291 ValidationError: [u'Enter a valid e-mail address.']
     1291ValidationError: ['Enter a valid e-mail address.']
    12921292>>> f.clean('foo@bar')
    12931293Traceback (most recent call last):
    12941294...
    1295 ValidationError: [u'Enter a valid e-mail address.']
     1295ValidationError: ['Enter a valid e-mail address.']
    12961296
    12971297>>> f = EmailField(required=False)
    12981298>>> f.clean('')
     
    13041304>>> f.clean('foo')
    13051305Traceback (most recent call last):
    13061306...
    1307 ValidationError: [u'Enter a valid e-mail address.']
     1307ValidationError: ['Enter a valid e-mail address.']
    13081308>>> f.clean('foo@')
    13091309Traceback (most recent call last):
    13101310...
    1311 ValidationError: [u'Enter a valid e-mail address.']
     1311ValidationError: ['Enter a valid e-mail address.']
    13121312>>> f.clean('foo@bar')
    13131313Traceback (most recent call last):
    13141314...
    1315 ValidationError: [u'Enter a valid e-mail address.']
     1315ValidationError: ['Enter a valid e-mail address.']
    13161316
    13171317EmailField also access min_length and max_length parameters, for convenience.
    13181318>>> f = EmailField(min_length=10, max_length=15)
    13191319>>> f.clean('a@foo.com')
    13201320Traceback (most recent call last):
    13211321...
    1322 ValidationError: [u'Ensure this value has at least 10 characters.']
     1322ValidationError: ['Ensure this value has at least 10 characters.']
    13231323>>> f.clean('alf@foo.com')
    13241324u'alf@foo.com'
    13251325>>> f.clean('alf123456788@foo.com')
    13261326Traceback (most recent call last):
    13271327...
    1328 ValidationError: [u'Ensure this value has at most 15 characters.']
     1328ValidationError: ['Ensure this value has at most 15 characters.']
    13291329
    13301330# URLField ##################################################################
    13311331
     
    13331333>>> f.clean('')
    13341334Traceback (most recent call last):
    13351335...
    1336 ValidationError: [u'This field is required.']
     1336ValidationError: ['This field is required.']
    13371337>>> f.clean(None)
    13381338Traceback (most recent call last):
    13391339...
    1340 ValidationError: [u'This field is required.']
     1340ValidationError: ['This field is required.']
    13411341>>> f.clean('http://example.com')
    13421342u'http://example.com'
    13431343>>> f.clean('http://www.example.com')
     
    13451345>>> f.clean('foo')
    13461346Traceback (most recent call last):
    13471347...
    1348 ValidationError: [u'Enter a valid URL.']
     1348ValidationError: ['Enter a valid URL.']
    13491349>>> f.clean('example.com')
    13501350Traceback (most recent call last):
    13511351...
    1352 ValidationError: [u'Enter a valid URL.']
     1352ValidationError: ['Enter a valid URL.']
    13531353>>> f.clean('http://')
    13541354Traceback (most recent call last):
    13551355...
    1356 ValidationError: [u'Enter a valid URL.']
     1356ValidationError: ['Enter a valid URL.']
    13571357>>> f.clean('http://example')
    13581358Traceback (most recent call last):
    13591359...
    1360 ValidationError: [u'Enter a valid URL.']
     1360ValidationError: ['Enter a valid URL.']
    13611361>>> f.clean('http://example.')
    13621362Traceback (most recent call last):
    13631363...
    1364 ValidationError: [u'Enter a valid URL.']
     1364ValidationError: ['Enter a valid URL.']
    13651365>>> f.clean('http://.com')
    13661366Traceback (most recent call last):
    13671367...
    1368 ValidationError: [u'Enter a valid URL.']
     1368ValidationError: ['Enter a valid URL.']
    13691369
    13701370>>> f = URLField(required=False)
    13711371>>> f.clean('')
     
    13791379>>> f.clean('foo')
    13801380Traceback (most recent call last):
    13811381...
    1382 ValidationError: [u'Enter a valid URL.']
     1382ValidationError: ['Enter a valid URL.']
    13831383>>> f.clean('example.com')
    13841384Traceback (most recent call last):
    13851385...
    1386 ValidationError: [u'Enter a valid URL.']
     1386ValidationError: ['Enter a valid URL.']
    13871387>>> f.clean('http://')
    13881388Traceback (most recent call last):
    13891389...
    1390 ValidationError: [u'Enter a valid URL.']
     1390ValidationError: ['Enter a valid URL.']
    13911391>>> f.clean('http://example')
    13921392Traceback (most recent call last):
    13931393...
    1394 ValidationError: [u'Enter a valid URL.']
     1394ValidationError: ['Enter a valid URL.']
    13951395>>> f.clean('http://example.')
    13961396Traceback (most recent call last):
    13971397...
    1398 ValidationError: [u'Enter a valid URL.']
     1398ValidationError: ['Enter a valid URL.']
    13991399>>> f.clean('http://.com')
    14001400Traceback (most recent call last):
    14011401...
    1402 ValidationError: [u'Enter a valid URL.']
     1402ValidationError: ['Enter a valid URL.']
    14031403
    14041404URLField takes an optional verify_exists parameter, which is False by default.
    14051405This verifies that the URL is live on the Internet and doesn't return a 404 or 500:
     
    14091409>>> f.clean('http://example')
    14101410Traceback (most recent call last):
    14111411...
    1412 ValidationError: [u'Enter a valid URL.']
     1412ValidationError: ['Enter a valid URL.']
    14131413>>> f.clean('http://www.jfoiwjfoi23jfoijoaijfoiwjofiwjefewl.com') # bad domain
    14141414Traceback (most recent call last):
    14151415...
    1416 ValidationError: [u'This URL appears to be a broken link.']
     1416ValidationError: ['This URL appears to be a broken link.']
    14171417>>> f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page
    14181418Traceback (most recent call last):
    14191419...
    1420 ValidationError: [u'This URL appears to be a broken link.']
     1420ValidationError: ['This URL appears to be a broken link.']
    14211421>>> f = URLField(verify_exists=True, required=False)
    14221422>>> f.clean('')
    14231423u''
     
    14291429>>> f.clean('http://f.com')
    14301430Traceback (most recent call last):
    14311431...
    1432 ValidationError: [u'Ensure this value has at least 15 characters.']
     1432ValidationError: ['Ensure this value has at least 15 characters.']
    14331433>>> f.clean('http://example.com')
    14341434u'http://example.com'
    14351435>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
    14361436Traceback (most recent call last):
    14371437...
    1438 ValidationError: [u'Ensure this value has at most 20 characters.']
     1438ValidationError: ['Ensure this value has at most 20 characters.']
    14391439
    14401440# BooleanField ################################################################
    14411441
     
    14431443>>> f.clean('')
    14441444Traceback (most recent call last):
    14451445...
    1446 ValidationError: [u'This field is required.']
     1446ValidationError: ['This field is required.']
    14471447>>> f.clean(None)
    14481448Traceback (most recent call last):
    14491449...
    1450 ValidationError: [u'This field is required.']
     1450ValidationError: ['This field is required.']
    14511451>>> f.clean(True)
    14521452True
    14531453>>> f.clean(False)
     
    14811481>>> f.clean('')
    14821482Traceback (most recent call last):
    14831483...
    1484 ValidationError: [u'This field is required.']
     1484ValidationError: ['This field is required.']
    14851485>>> f.clean(None)
    14861486Traceback (most recent call last):
    14871487...
    1488 ValidationError: [u'This field is required.']
     1488ValidationError: ['This field is required.']
    14891489>>> f.clean(1)
    14901490u'1'
    14911491>>> f.clean('1')
     
    14931493>>> f.clean('3')
    14941494Traceback (most recent call last):
    14951495...
    1496 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
     1496ValidationError: ['Select a valid choice. 3 is not one of the available choices.']
    14971497
    14981498>>> f = ChoiceField(choices=[('1', '1'), ('2', '2')], required=False)
    14991499>>> f.clean('')
     
    15071507>>> f.clean('3')
    15081508Traceback (most recent call last):
    15091509...
    1510 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
     1510ValidationError: ['Select a valid choice. 3 is not one of the available choices.']
    15111511
    15121512>>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')])
    15131513>>> f.clean('J')
     
    15151515>>> f.clean('John')
    15161516Traceback (most recent call last):
    15171517...
    1518 ValidationError: [u'Select a valid choice. John is not one of the available choices.']
     1518ValidationError: ['Select a valid choice. John is not one of the available choices.']
    15191519
    15201520# NullBooleanField ############################################################
    15211521
     
    15371537>>> f.clean('')
    15381538Traceback (most recent call last):
    15391539...
    1540 ValidationError: [u'This field is required.']
     1540ValidationError: ['This field is required.']
    15411541>>> f.clean(None)
    15421542Traceback (most recent call last):
    15431543...
    1544 ValidationError: [u'This field is required.']
     1544ValidationError: ['This field is required.']
    15451545>>> f.clean([1])
    15461546[u'1']
    15471547>>> f.clean(['1'])
     
    15551555>>> f.clean('hello')
    15561556Traceback (most recent call last):
    15571557...
    1558 ValidationError: [u'Enter a list of values.']
     1558ValidationError: ['Enter a list of values.']
    15591559>>> f.clean([])
    15601560Traceback (most recent call last):
    15611561...
    1562 ValidationError: [u'This field is required.']
     1562ValidationError: ['This field is required.']
    15631563>>> f.clean(())
    15641564Traceback (most recent call last):
    15651565...
    1566 ValidationError: [u'This field is required.']
     1566ValidationError: ['This field is required.']
    15671567>>> f.clean(['3'])
    15681568Traceback (most recent call last):
    15691569...
    1570 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
     1570ValidationError: ['Select a valid choice. 3 is not one of the available choices.']
    15711571
    15721572>>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')], required=False)
    15731573>>> f.clean('')
     
    15871587>>> f.clean('hello')
    15881588Traceback (most recent call last):
    15891589...
    1590 ValidationError: [u'Enter a list of values.']
     1590ValidationError: ['Enter a list of values.']
    15911591>>> f.clean([])
    15921592[]
    15931593>>> f.clean(())
     
    15951595>>> f.clean(['3'])
    15961596Traceback (most recent call last):
    15971597...
    1598 ValidationError: [u'Select a valid choice. 3 is not one of the available choices.']
     1598ValidationError: ['Select a valid choice. 3 is not one of the available choices.']
    15991599
    16001600# ComboField ##################################################################
    16011601
     
    16071607>>> f.clean('longemailaddress@example.com')
    16081608Traceback (most recent call last):
    16091609...
    1610 ValidationError: [u'Ensure this value has at most 20 characters.']
     1610ValidationError: ['Ensure this value has at most 20 characters.']
    16111611>>> f.clean('not an e-mail')
    16121612Traceback (most recent call last):
    16131613...
    1614 ValidationError: [u'Enter a valid e-mail address.']
     1614ValidationError: ['Enter a valid e-mail address.']
    16151615>>> f.clean('')
    16161616Traceback (most recent call last):
    16171617...
    1618 ValidationError: [u'This field is required.']
     1618ValidationError: ['This field is required.']
    16191619>>> f.clean(None)
    16201620Traceback (most recent call last):
    16211621...
    1622 ValidationError: [u'This field is required.']
     1622ValidationError: ['This field is required.']
    16231623
    16241624>>> f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False)
    16251625>>> f.clean('test@example.com')
     
    16271627>>> f.clean('longemailaddress@example.com')
    16281628Traceback (most recent call last):
    16291629...
    1630 ValidationError: [u'Ensure this value has at most 20 characters.']
     1630ValidationError: ['Ensure this value has at most 20 characters.']
    16311631>>> f.clean('not an e-mail')
    16321632Traceback (most recent call last):
    16331633...
    1634 ValidationError: [u'Enter a valid e-mail address.']
     1634ValidationError: ['Enter a valid e-mail address.']
    16351635>>> f.clean('')
    16361636u''
    16371637>>> f.clean(None)
     
    16451645>>> f.clean(None)
    16461646Traceback (most recent call last):
    16471647...
    1648 ValidationError: [u'This field is required.']
     1648ValidationError: ['This field is required.']
    16491649>>> f.clean('')
    16501650Traceback (most recent call last):
    16511651...
    1652 ValidationError: [u'This field is required.']
     1652ValidationError: ['This field is required.']
    16531653>>> f.clean('hello')
    16541654Traceback (most recent call last):
    16551655...
    1656 ValidationError: [u'Enter a list of values.']
     1656ValidationError: ['Enter a list of values.']
    16571657>>> f.clean(['hello', 'there'])
    16581658Traceback (most recent call last):
    16591659...
    1660 ValidationError: [u'Enter a valid date.', u'Enter a valid time.']
     1660ValidationError: ['Enter a valid date.', 'Enter a valid time.']
    16611661>>> f.clean(['2006-01-10', 'there'])
    16621662Traceback (most recent call last):
    16631663...
    1664 ValidationError: [u'Enter a valid time.']
     1664ValidationError: ['Enter a valid time.']
    16651665>>> f.clean(['hello', '07:30'])
    16661666Traceback (most recent call last):
    16671667...
    1668 ValidationError: [u'Enter a valid date.']
     1668ValidationError: ['Enter a valid date.']
    16691669
    16701670>>> f = SplitDateTimeField(required=False)
    16711671>>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)])
     
    16751675>>> f.clean('hello')
    16761676Traceback (most recent call last):
    16771677...
    1678 ValidationError: [u'Enter a list of values.']
     1678ValidationError: ['Enter a list of values.']
    16791679>>> f.clean(['hello', 'there'])
    16801680Traceback (most recent call last):
    16811681...
    1682 ValidationError: [u'Enter a valid date.', u'Enter a valid time.']
     1682ValidationError: ['Enter a valid date.', 'Enter a valid time.']
    16831683>>> f.clean(['2006-01-10', 'there'])
    16841684Traceback (most recent call last):
    16851685...
    1686 ValidationError: [u'Enter a valid time.']
     1686ValidationError: ['Enter a valid time.']
    16871687>>> f.clean(['hello', '07:30'])
    16881688Traceback (most recent call last):
    16891689...
    1690 ValidationError: [u'Enter a valid date.']
     1690ValidationError: ['Enter a valid date.']
    16911691
    16921692#########
    16931693# Forms #
     
    17491749>>> p.is_bound
    17501750True
    17511751>>> p.errors
    1752 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
     1752{'first_name': ['This field is required.'], 'last_name': ['This field is required.'], 'birthday': ['This field is required.']}
    17531753>>> p.is_valid()
    17541754False
    17551755>>> p.clean_data
     
    18181818
    18191819>>> p = Person({'last_name': u'Lennon'})
    18201820>>> p.errors
    1821 {'first_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
     1821{'first_name': ['This field is required.'], 'birthday': ['This field is required.']}
    18221822>>> p.is_valid()
    18231823False
    18241824>>> p.errors.as_ul()
     
    18331833...
    18341834AttributeError: 'Person' object has no attribute 'clean_data'
    18351835>>> p['first_name'].errors
    1836 [u'This field is required.']
     1836['This field is required.']
    18371837>>> p['first_name'].errors.as_ul()
    18381838u'<ul class="errorlist"><li>This field is required.</li></ul>'
    18391839>>> p['first_name'].errors.as_text()
     
    21892189returns a list of input.
    21902190>>> f = SongForm({'name': 'Yesterday'}, auto_id=False)
    21912191>>> f.errors
    2192 {'composers': [u'This field is required.']}
     2192{'composers': ['This field is required.']}
    21932193>>> f = SongForm({'name': 'Yesterday', 'composers': ['J']}, auto_id=False)
    21942194>>> f.errors
    21952195{}
     
    22232223{}
    22242224>>> f = UserRegistration({}, auto_id=False)
    22252225>>> f.errors
    2226 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
     2226{'username': ['This field is required.'], 'password1': ['This field is required.'], 'password2': ['This field is required.']}
    22272227>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False)
    22282228>>> f.errors
    2229 {'password2': [u'Please make sure your passwords match.']}
     2229{'password2': ['Please make sure your passwords match.']}
    22302230>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False)
    22312231>>> f.errors
    22322232{}
     
    22572257<tr><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr>
    22582258<tr><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr>
    22592259>>> f.errors
    2260 {'username': [u'This field is required.'], 'password1': [u'This field is required.'], 'password2': [u'This field is required.']}
     2260{'username': ['This field is required.'], 'password1': ['This field is required.'], 'password2': ['This field is required.']}
    22612261>>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False)
    22622262>>> f.errors
    2263 {'__all__': [u'Please make sure your passwords match.']}
     2263{'__all__': ['Please make sure your passwords match.']}
    22642264>>> print f.as_table()
    22652265<tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr>
    22662266<tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr>
     
    25512551validation error rather than using the initial value for 'username'.
    25522552>>> p = UserRegistration({'password': 'secret'})
    25532553>>> p.errors
    2554 {'username': [u'This field is required.']}
     2554{'username': ['This field is required.']}
    25552555>>> p.is_valid()
    25562556False
    25572557
     
    25962596validation error rather than using the initial value for 'username'.
    25972597>>> p = UserRegistration({'password': 'secret'}, initial={'username': 'django'})
    25982598>>> p.errors
    2599 {'username': [u'This field is required.']}
     2599{'username': ['This field is required.']}
    26002600>>> p.is_valid()
    26012601False
    26022602
     
    26552655... }
    26562656>>> p = Person(data, prefix='person1')
    26572657>>> p.errors
    2658 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
     2658{'first_name': ['This field is required.'], 'last_name': ['This field is required.'], 'birthday': ['This field is required.']}
    26592659>>> p['first_name'].errors
    2660 [u'This field is required.']
     2660['This field is required.']
    26612661>>> p['person1-first_name'].errors
    26622662Traceback (most recent call last):
    26632663...
     
    26722672... }
    26732673>>> p = Person(data, prefix='person1')
    26742674>>> p.errors
    2675 {'first_name': [u'This field is required.'], 'last_name': [u'This field is required.'], 'birthday': [u'This field is required.']}
     2675{'first_name': ['This field is required.'], 'last_name': ['This field is required.'], 'birthday': ['This field is required.']}
    26762676
    26772677With prefixes, a single data dictionary can hold data for multiple instances
    26782678of the same form.
Back to Top