Ticket #3395: unicode_newforms_errors_new.patch
File unicode_newforms_errors_new.patch, 28.6 KB (added by , 18 years ago) |
---|
-
django/newforms/util.py
5 5 # a leading space. Assumes keys do not need to be XML-escaped. 6 6 flatatt = lambda attrs: u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()]) 7 7 8 def 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 8 15 def smart_unicode(s): 9 16 if not isinstance(s, basestring): 10 17 s = unicode(str(s)) … … 57 64 def __init__(self, message): 58 65 "ValidationError can be passed a string or a list." 59 66 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]) 61 68 else: 62 69 assert isinstance(message, basestring), ("%s should be a basestring" % repr(message)) 63 message = smart_ unicode(message)70 message = smart_deunicode(message) 64 71 self.messages = ErrorList([message]) 65 72 66 73 def __str__(self): -
tests/modeltests/model_forms/models.py
112 112 If you call save() with invalid data, you'll get a ValueError. 113 113 >>> f = CategoryForm({'name': '', 'url': 'foo'}) 114 114 >>> f.errors 115 {'name': [ u'This field is required.']}115 {'name': ['This field is required.']} 116 116 >>> f.clean_data 117 117 Traceback (most recent call last): 118 118 ... -
tests/regressiontests/forms/tests.py
805 805 >>> f.clean(None) 806 806 Traceback (most recent call last): 807 807 ... 808 ValidationError: [ u'This field is required.']808 ValidationError: ['This field is required.'] 809 809 >>> f.clean('') 810 810 Traceback (most recent call last): 811 811 ... 812 ValidationError: [ u'This field is required.']812 ValidationError: ['This field is required.'] 813 813 >>> f.clean([1, 2, 3]) 814 814 u'[1, 2, 3]' 815 815 … … 834 834 >>> f.clean('1234567890a') 835 835 Traceback (most recent call last): 836 836 ... 837 ValidationError: [ u'Ensure this value has at most 10 characters.']837 ValidationError: ['Ensure this value has at most 10 characters.'] 838 838 839 839 CharField accepts an optional min_length parameter: 840 840 >>> f = CharField(min_length=10, required=False) … … 843 843 >>> f.clean('12345') 844 844 Traceback (most recent call last): 845 845 ... 846 ValidationError: [ u'Ensure this value has at least 10 characters.']846 ValidationError: ['Ensure this value has at least 10 characters.'] 847 847 >>> f.clean('1234567890') 848 848 u'1234567890' 849 849 >>> f.clean('1234567890a') … … 853 853 >>> f.clean('') 854 854 Traceback (most recent call last): 855 855 ... 856 ValidationError: [ u'This field is required.']856 ValidationError: ['This field is required.'] 857 857 >>> f.clean('12345') 858 858 Traceback (most recent call last): 859 859 ... 860 ValidationError: [ u'Ensure this value has at least 10 characters.']860 ValidationError: ['Ensure this value has at least 10 characters.'] 861 861 >>> f.clean('1234567890') 862 862 u'1234567890' 863 863 >>> f.clean('1234567890a') … … 869 869 >>> f.clean('') 870 870 Traceback (most recent call last): 871 871 ... 872 ValidationError: [ u'This field is required.']872 ValidationError: ['This field is required.'] 873 873 >>> f.clean(None) 874 874 Traceback (most recent call last): 875 875 ... 876 ValidationError: [ u'This field is required.']876 ValidationError: ['This field is required.'] 877 877 >>> f.clean('1') 878 878 1 879 879 >>> isinstance(f.clean('1'), int) … … 883 883 >>> f.clean('a') 884 884 Traceback (most recent call last): 885 885 ... 886 ValidationError: [ u'Enter a whole number.']886 ValidationError: ['Enter a whole number.'] 887 887 >>> f.clean('1 ') 888 888 1 889 889 >>> f.clean(' 1') … … 893 893 >>> f.clean('1a') 894 894 Traceback (most recent call last): 895 895 ... 896 ValidationError: [ u'Enter a whole number.']896 ValidationError: ['Enter a whole number.'] 897 897 898 898 >>> f = IntegerField(required=False) 899 899 >>> f.clean('') … … 911 911 >>> f.clean('a') 912 912 Traceback (most recent call last): 913 913 ... 914 ValidationError: [ u'Enter a whole number.']914 ValidationError: ['Enter a whole number.'] 915 915 >>> f.clean('1 ') 916 916 1 917 917 >>> f.clean(' 1') … … 921 921 >>> f.clean('1a') 922 922 Traceback (most recent call last): 923 923 ... 924 ValidationError: [ u'Enter a whole number.']924 ValidationError: ['Enter a whole number.'] 925 925 926 926 IntegerField accepts an optional max_value parameter: 927 927 >>> f = IntegerField(max_value=10) 928 928 >>> f.clean(None) 929 929 Traceback (most recent call last): 930 930 ... 931 ValidationError: [ u'This field is required.']931 ValidationError: ['This field is required.'] 932 932 >>> f.clean(1) 933 933 1 934 934 >>> f.clean(10) … … 936 936 >>> f.clean(11) 937 937 Traceback (most recent call last): 938 938 ... 939 ValidationError: [ u'Ensure this value is less than or equal to 10.']939 ValidationError: ['Ensure this value is less than or equal to 10.'] 940 940 >>> f.clean('10') 941 941 10 942 942 >>> f.clean('11') 943 943 Traceback (most recent call last): 944 944 ... 945 ValidationError: [ u'Ensure this value is less than or equal to 10.']945 ValidationError: ['Ensure this value is less than or equal to 10.'] 946 946 947 947 IntegerField accepts an optional min_value parameter: 948 948 >>> f = IntegerField(min_value=10) 949 949 >>> f.clean(None) 950 950 Traceback (most recent call last): 951 951 ... 952 ValidationError: [ u'This field is required.']952 ValidationError: ['This field is required.'] 953 953 >>> f.clean(1) 954 954 Traceback (most recent call last): 955 955 ... 956 ValidationError: [ u'Ensure this value is greater than or equal to 10.']956 ValidationError: ['Ensure this value is greater than or equal to 10.'] 957 957 >>> f.clean(10) 958 958 10 959 959 >>> f.clean(11) … … 968 968 >>> f.clean(None) 969 969 Traceback (most recent call last): 970 970 ... 971 ValidationError: [ u'This field is required.']971 ValidationError: ['This field is required.'] 972 972 >>> f.clean(1) 973 973 Traceback (most recent call last): 974 974 ... 975 ValidationError: [ u'Ensure this value is greater than or equal to 10.']975 ValidationError: ['Ensure this value is greater than or equal to 10.'] 976 976 >>> f.clean(10) 977 977 10 978 978 >>> f.clean(11) … … 986 986 >>> f.clean(21) 987 987 Traceback (most recent call last): 988 988 ... 989 ValidationError: [ u'Ensure this value is less than or equal to 20.']989 ValidationError: ['Ensure this value is less than or equal to 20.'] 990 990 991 991 # DateField ################################################################### 992 992 … … 1019 1019 >>> f.clean('2006-4-31') 1020 1020 Traceback (most recent call last): 1021 1021 ... 1022 ValidationError: [ u'Enter a valid date.']1022 ValidationError: ['Enter a valid date.'] 1023 1023 >>> f.clean('200a-10-25') 1024 1024 Traceback (most recent call last): 1025 1025 ... 1026 ValidationError: [ u'Enter a valid date.']1026 ValidationError: ['Enter a valid date.'] 1027 1027 >>> f.clean('25/10/06') 1028 1028 Traceback (most recent call last): 1029 1029 ... 1030 ValidationError: [ u'Enter a valid date.']1030 ValidationError: ['Enter a valid date.'] 1031 1031 >>> f.clean(None) 1032 1032 Traceback (most recent call last): 1033 1033 ... 1034 ValidationError: [ u'This field is required.']1034 ValidationError: ['This field is required.'] 1035 1035 1036 1036 >>> f = DateField(required=False) 1037 1037 >>> f.clean(None) … … 1055 1055 >>> f.clean('2006-10-25') 1056 1056 Traceback (most recent call last): 1057 1057 ... 1058 ValidationError: [ u'Enter a valid date.']1058 ValidationError: ['Enter a valid date.'] 1059 1059 >>> f.clean('10/25/2006') 1060 1060 Traceback (most recent call last): 1061 1061 ... 1062 ValidationError: [ u'Enter a valid date.']1062 ValidationError: ['Enter a valid date.'] 1063 1063 >>> f.clean('10/25/06') 1064 1064 Traceback (most recent call last): 1065 1065 ... 1066 ValidationError: [ u'Enter a valid date.']1066 ValidationError: ['Enter a valid date.'] 1067 1067 1068 1068 # TimeField ################################################################### 1069 1069 … … 1080 1080 >>> f.clean('hello') 1081 1081 Traceback (most recent call last): 1082 1082 ... 1083 ValidationError: [ u'Enter a valid time.']1083 ValidationError: ['Enter a valid time.'] 1084 1084 >>> f.clean('1:24 p.m.') 1085 1085 Traceback (most recent call last): 1086 1086 ... 1087 ValidationError: [ u'Enter a valid time.']1087 ValidationError: ['Enter a valid time.'] 1088 1088 1089 1089 TimeField accepts an optional input_formats parameter: 1090 1090 >>> f = TimeField(input_formats=['%I:%M %p']) … … 1102 1102 >>> f.clean('14:30:45') 1103 1103 Traceback (most recent call last): 1104 1104 ... 1105 ValidationError: [ u'Enter a valid time.']1105 ValidationError: ['Enter a valid time.'] 1106 1106 1107 1107 # DateTimeField ############################################################### 1108 1108 … … 1143 1143 >>> f.clean('hello') 1144 1144 Traceback (most recent call last): 1145 1145 ... 1146 ValidationError: [ u'Enter a valid date/time.']1146 ValidationError: ['Enter a valid date/time.'] 1147 1147 >>> f.clean('2006-10-25 4:30 p.m.') 1148 1148 Traceback (most recent call last): 1149 1149 ... 1150 ValidationError: [ u'Enter a valid date/time.']1150 ValidationError: ['Enter a valid date/time.'] 1151 1151 1152 1152 DateField accepts an optional input_formats parameter: 1153 1153 >>> f = DateTimeField(input_formats=['%Y %m %d %I:%M %p']) … … 1167 1167 >>> f.clean('2006-10-25 14:30:45') 1168 1168 Traceback (most recent call last): 1169 1169 ... 1170 ValidationError: [ u'Enter a valid date/time.']1170 ValidationError: ['Enter a valid date/time.'] 1171 1171 1172 1172 >>> f = DateTimeField(required=False) 1173 1173 >>> f.clean(None) … … 1187 1187 >>> f.clean('3G3') 1188 1188 Traceback (most recent call last): 1189 1189 ... 1190 ValidationError: [ u'Enter a valid value.']1190 ValidationError: ['Enter a valid value.'] 1191 1191 >>> f.clean(' 2A2') 1192 1192 Traceback (most recent call last): 1193 1193 ... 1194 ValidationError: [ u'Enter a valid value.']1194 ValidationError: ['Enter a valid value.'] 1195 1195 >>> f.clean('2A2 ') 1196 1196 Traceback (most recent call last): 1197 1197 ... 1198 ValidationError: [ u'Enter a valid value.']1198 ValidationError: ['Enter a valid value.'] 1199 1199 >>> f.clean('') 1200 1200 Traceback (most recent call last): 1201 1201 ... 1202 ValidationError: [ u'This field is required.']1202 ValidationError: ['This field is required.'] 1203 1203 1204 1204 >>> f = RegexField('^\d[A-F]\d$', required=False) 1205 1205 >>> f.clean('2A2') … … 1209 1209 >>> f.clean('3G3') 1210 1210 Traceback (most recent call last): 1211 1211 ... 1212 ValidationError: [ u'Enter a valid value.']1212 ValidationError: ['Enter a valid value.'] 1213 1213 >>> f.clean('') 1214 1214 u'' 1215 1215 … … 1222 1222 >>> f.clean('3G3') 1223 1223 Traceback (most recent call last): 1224 1224 ... 1225 ValidationError: [ u'Enter a valid value.']1225 ValidationError: ['Enter a valid value.'] 1226 1226 >>> f.clean(' 2A2') 1227 1227 Traceback (most recent call last): 1228 1228 ... 1229 ValidationError: [ u'Enter a valid value.']1229 ValidationError: ['Enter a valid value.'] 1230 1230 >>> f.clean('2A2 ') 1231 1231 Traceback (most recent call last): 1232 1232 ... 1233 ValidationError: [ u'Enter a valid value.']1233 ValidationError: ['Enter a valid value.'] 1234 1234 1235 1235 RegexField takes an optional error_message argument: 1236 1236 >>> f = RegexField('^\d\d\d\d$', error_message='Enter a four-digit number.') … … 1239 1239 >>> f.clean('123') 1240 1240 Traceback (most recent call last): 1241 1241 ... 1242 ValidationError: [ u'Enter a four-digit number.']1242 ValidationError: ['Enter a four-digit number.'] 1243 1243 >>> f.clean('abcd') 1244 1244 Traceback (most recent call last): 1245 1245 ... 1246 ValidationError: [ u'Enter a four-digit number.']1246 ValidationError: ['Enter a four-digit number.'] 1247 1247 1248 1248 RegexField also access min_length and max_length parameters, for convenience. 1249 1249 >>> f = RegexField('^\d+$', min_length=5, max_length=10) 1250 1250 >>> f.clean('123') 1251 1251 Traceback (most recent call last): 1252 1252 ... 1253 ValidationError: [ u'Ensure this value has at least 5 characters.']1253 ValidationError: ['Ensure this value has at least 5 characters.'] 1254 1254 >>> f.clean('abc') 1255 1255 Traceback (most recent call last): 1256 1256 ... 1257 ValidationError: [ u'Ensure this value has at least 5 characters.']1257 ValidationError: ['Ensure this value has at least 5 characters.'] 1258 1258 >>> f.clean('12345') 1259 1259 u'12345' 1260 1260 >>> f.clean('1234567890') … … 1262 1262 >>> f.clean('12345678901') 1263 1263 Traceback (most recent call last): 1264 1264 ... 1265 ValidationError: [ u'Ensure this value has at most 10 characters.']1265 ValidationError: ['Ensure this value has at most 10 characters.'] 1266 1266 >>> f.clean('12345a') 1267 1267 Traceback (most recent call last): 1268 1268 ... 1269 ValidationError: [ u'Enter a valid value.']1269 ValidationError: ['Enter a valid value.'] 1270 1270 1271 1271 # EmailField ################################################################## 1272 1272 … … 1274 1274 >>> f.clean('') 1275 1275 Traceback (most recent call last): 1276 1276 ... 1277 ValidationError: [ u'This field is required.']1277 ValidationError: ['This field is required.'] 1278 1278 >>> f.clean(None) 1279 1279 Traceback (most recent call last): 1280 1280 ... 1281 ValidationError: [ u'This field is required.']1281 ValidationError: ['This field is required.'] 1282 1282 >>> f.clean('person@example.com') 1283 1283 u'person@example.com' 1284 1284 >>> f.clean('foo') 1285 1285 Traceback (most recent call last): 1286 1286 ... 1287 ValidationError: [ u'Enter a valid e-mail address.']1287 ValidationError: ['Enter a valid e-mail address.'] 1288 1288 >>> f.clean('foo@') 1289 1289 Traceback (most recent call last): 1290 1290 ... 1291 ValidationError: [ u'Enter a valid e-mail address.']1291 ValidationError: ['Enter a valid e-mail address.'] 1292 1292 >>> f.clean('foo@bar') 1293 1293 Traceback (most recent call last): 1294 1294 ... 1295 ValidationError: [ u'Enter a valid e-mail address.']1295 ValidationError: ['Enter a valid e-mail address.'] 1296 1296 1297 1297 >>> f = EmailField(required=False) 1298 1298 >>> f.clean('') … … 1304 1304 >>> f.clean('foo') 1305 1305 Traceback (most recent call last): 1306 1306 ... 1307 ValidationError: [ u'Enter a valid e-mail address.']1307 ValidationError: ['Enter a valid e-mail address.'] 1308 1308 >>> f.clean('foo@') 1309 1309 Traceback (most recent call last): 1310 1310 ... 1311 ValidationError: [ u'Enter a valid e-mail address.']1311 ValidationError: ['Enter a valid e-mail address.'] 1312 1312 >>> f.clean('foo@bar') 1313 1313 Traceback (most recent call last): 1314 1314 ... 1315 ValidationError: [ u'Enter a valid e-mail address.']1315 ValidationError: ['Enter a valid e-mail address.'] 1316 1316 1317 1317 EmailField also access min_length and max_length parameters, for convenience. 1318 1318 >>> f = EmailField(min_length=10, max_length=15) 1319 1319 >>> f.clean('a@foo.com') 1320 1320 Traceback (most recent call last): 1321 1321 ... 1322 ValidationError: [ u'Ensure this value has at least 10 characters.']1322 ValidationError: ['Ensure this value has at least 10 characters.'] 1323 1323 >>> f.clean('alf@foo.com') 1324 1324 u'alf@foo.com' 1325 1325 >>> f.clean('alf123456788@foo.com') 1326 1326 Traceback (most recent call last): 1327 1327 ... 1328 ValidationError: [ u'Ensure this value has at most 15 characters.']1328 ValidationError: ['Ensure this value has at most 15 characters.'] 1329 1329 1330 1330 # URLField ################################################################## 1331 1331 … … 1333 1333 >>> f.clean('') 1334 1334 Traceback (most recent call last): 1335 1335 ... 1336 ValidationError: [ u'This field is required.']1336 ValidationError: ['This field is required.'] 1337 1337 >>> f.clean(None) 1338 1338 Traceback (most recent call last): 1339 1339 ... 1340 ValidationError: [ u'This field is required.']1340 ValidationError: ['This field is required.'] 1341 1341 >>> f.clean('http://example.com') 1342 1342 u'http://example.com' 1343 1343 >>> f.clean('http://www.example.com') … … 1345 1345 >>> f.clean('foo') 1346 1346 Traceback (most recent call last): 1347 1347 ... 1348 ValidationError: [ u'Enter a valid URL.']1348 ValidationError: ['Enter a valid URL.'] 1349 1349 >>> f.clean('example.com') 1350 1350 Traceback (most recent call last): 1351 1351 ... 1352 ValidationError: [ u'Enter a valid URL.']1352 ValidationError: ['Enter a valid URL.'] 1353 1353 >>> f.clean('http://') 1354 1354 Traceback (most recent call last): 1355 1355 ... 1356 ValidationError: [ u'Enter a valid URL.']1356 ValidationError: ['Enter a valid URL.'] 1357 1357 >>> f.clean('http://example') 1358 1358 Traceback (most recent call last): 1359 1359 ... 1360 ValidationError: [ u'Enter a valid URL.']1360 ValidationError: ['Enter a valid URL.'] 1361 1361 >>> f.clean('http://example.') 1362 1362 Traceback (most recent call last): 1363 1363 ... 1364 ValidationError: [ u'Enter a valid URL.']1364 ValidationError: ['Enter a valid URL.'] 1365 1365 >>> f.clean('http://.com') 1366 1366 Traceback (most recent call last): 1367 1367 ... 1368 ValidationError: [ u'Enter a valid URL.']1368 ValidationError: ['Enter a valid URL.'] 1369 1369 1370 1370 >>> f = URLField(required=False) 1371 1371 >>> f.clean('') … … 1379 1379 >>> f.clean('foo') 1380 1380 Traceback (most recent call last): 1381 1381 ... 1382 ValidationError: [ u'Enter a valid URL.']1382 ValidationError: ['Enter a valid URL.'] 1383 1383 >>> f.clean('example.com') 1384 1384 Traceback (most recent call last): 1385 1385 ... 1386 ValidationError: [ u'Enter a valid URL.']1386 ValidationError: ['Enter a valid URL.'] 1387 1387 >>> f.clean('http://') 1388 1388 Traceback (most recent call last): 1389 1389 ... 1390 ValidationError: [ u'Enter a valid URL.']1390 ValidationError: ['Enter a valid URL.'] 1391 1391 >>> f.clean('http://example') 1392 1392 Traceback (most recent call last): 1393 1393 ... 1394 ValidationError: [ u'Enter a valid URL.']1394 ValidationError: ['Enter a valid URL.'] 1395 1395 >>> f.clean('http://example.') 1396 1396 Traceback (most recent call last): 1397 1397 ... 1398 ValidationError: [ u'Enter a valid URL.']1398 ValidationError: ['Enter a valid URL.'] 1399 1399 >>> f.clean('http://.com') 1400 1400 Traceback (most recent call last): 1401 1401 ... 1402 ValidationError: [ u'Enter a valid URL.']1402 ValidationError: ['Enter a valid URL.'] 1403 1403 1404 1404 URLField takes an optional verify_exists parameter, which is False by default. 1405 1405 This verifies that the URL is live on the Internet and doesn't return a 404 or 500: … … 1409 1409 >>> f.clean('http://example') 1410 1410 Traceback (most recent call last): 1411 1411 ... 1412 ValidationError: [ u'Enter a valid URL.']1412 ValidationError: ['Enter a valid URL.'] 1413 1413 >>> f.clean('http://www.jfoiwjfoi23jfoijoaijfoiwjofiwjefewl.com') # bad domain 1414 1414 Traceback (most recent call last): 1415 1415 ... 1416 ValidationError: [ u'This URL appears to be a broken link.']1416 ValidationError: ['This URL appears to be a broken link.'] 1417 1417 >>> f.clean('http://google.com/we-love-microsoft.html') # good domain, bad page 1418 1418 Traceback (most recent call last): 1419 1419 ... 1420 ValidationError: [ u'This URL appears to be a broken link.']1420 ValidationError: ['This URL appears to be a broken link.'] 1421 1421 >>> f = URLField(verify_exists=True, required=False) 1422 1422 >>> f.clean('') 1423 1423 u'' … … 1429 1429 >>> f.clean('http://f.com') 1430 1430 Traceback (most recent call last): 1431 1431 ... 1432 ValidationError: [ u'Ensure this value has at least 15 characters.']1432 ValidationError: ['Ensure this value has at least 15 characters.'] 1433 1433 >>> f.clean('http://example.com') 1434 1434 u'http://example.com' 1435 1435 >>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com') 1436 1436 Traceback (most recent call last): 1437 1437 ... 1438 ValidationError: [ u'Ensure this value has at most 20 characters.']1438 ValidationError: ['Ensure this value has at most 20 characters.'] 1439 1439 1440 1440 # BooleanField ################################################################ 1441 1441 … … 1443 1443 >>> f.clean('') 1444 1444 Traceback (most recent call last): 1445 1445 ... 1446 ValidationError: [ u'This field is required.']1446 ValidationError: ['This field is required.'] 1447 1447 >>> f.clean(None) 1448 1448 Traceback (most recent call last): 1449 1449 ... 1450 ValidationError: [ u'This field is required.']1450 ValidationError: ['This field is required.'] 1451 1451 >>> f.clean(True) 1452 1452 True 1453 1453 >>> f.clean(False) … … 1481 1481 >>> f.clean('') 1482 1482 Traceback (most recent call last): 1483 1483 ... 1484 ValidationError: [ u'This field is required.']1484 ValidationError: ['This field is required.'] 1485 1485 >>> f.clean(None) 1486 1486 Traceback (most recent call last): 1487 1487 ... 1488 ValidationError: [ u'This field is required.']1488 ValidationError: ['This field is required.'] 1489 1489 >>> f.clean(1) 1490 1490 u'1' 1491 1491 >>> f.clean('1') … … 1493 1493 >>> f.clean('3') 1494 1494 Traceback (most recent call last): 1495 1495 ... 1496 ValidationError: [ u'Select a valid choice. 3 is not one of the available choices.']1496 ValidationError: ['Select a valid choice. 3 is not one of the available choices.'] 1497 1497 1498 1498 >>> f = ChoiceField(choices=[('1', '1'), ('2', '2')], required=False) 1499 1499 >>> f.clean('') … … 1507 1507 >>> f.clean('3') 1508 1508 Traceback (most recent call last): 1509 1509 ... 1510 ValidationError: [ u'Select a valid choice. 3 is not one of the available choices.']1510 ValidationError: ['Select a valid choice. 3 is not one of the available choices.'] 1511 1511 1512 1512 >>> f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')]) 1513 1513 >>> f.clean('J') … … 1515 1515 >>> f.clean('John') 1516 1516 Traceback (most recent call last): 1517 1517 ... 1518 ValidationError: [ u'Select a valid choice. John is not one of the available choices.']1518 ValidationError: ['Select a valid choice. John is not one of the available choices.'] 1519 1519 1520 1520 # NullBooleanField ############################################################ 1521 1521 … … 1537 1537 >>> f.clean('') 1538 1538 Traceback (most recent call last): 1539 1539 ... 1540 ValidationError: [ u'This field is required.']1540 ValidationError: ['This field is required.'] 1541 1541 >>> f.clean(None) 1542 1542 Traceback (most recent call last): 1543 1543 ... 1544 ValidationError: [ u'This field is required.']1544 ValidationError: ['This field is required.'] 1545 1545 >>> f.clean([1]) 1546 1546 [u'1'] 1547 1547 >>> f.clean(['1']) … … 1555 1555 >>> f.clean('hello') 1556 1556 Traceback (most recent call last): 1557 1557 ... 1558 ValidationError: [ u'Enter a list of values.']1558 ValidationError: ['Enter a list of values.'] 1559 1559 >>> f.clean([]) 1560 1560 Traceback (most recent call last): 1561 1561 ... 1562 ValidationError: [ u'This field is required.']1562 ValidationError: ['This field is required.'] 1563 1563 >>> f.clean(()) 1564 1564 Traceback (most recent call last): 1565 1565 ... 1566 ValidationError: [ u'This field is required.']1566 ValidationError: ['This field is required.'] 1567 1567 >>> f.clean(['3']) 1568 1568 Traceback (most recent call last): 1569 1569 ... 1570 ValidationError: [ u'Select a valid choice. 3 is not one of the available choices.']1570 ValidationError: ['Select a valid choice. 3 is not one of the available choices.'] 1571 1571 1572 1572 >>> f = MultipleChoiceField(choices=[('1', '1'), ('2', '2')], required=False) 1573 1573 >>> f.clean('') … … 1587 1587 >>> f.clean('hello') 1588 1588 Traceback (most recent call last): 1589 1589 ... 1590 ValidationError: [ u'Enter a list of values.']1590 ValidationError: ['Enter a list of values.'] 1591 1591 >>> f.clean([]) 1592 1592 [] 1593 1593 >>> f.clean(()) … … 1595 1595 >>> f.clean(['3']) 1596 1596 Traceback (most recent call last): 1597 1597 ... 1598 ValidationError: [ u'Select a valid choice. 3 is not one of the available choices.']1598 ValidationError: ['Select a valid choice. 3 is not one of the available choices.'] 1599 1599 1600 1600 # ComboField ################################################################## 1601 1601 … … 1607 1607 >>> f.clean('longemailaddress@example.com') 1608 1608 Traceback (most recent call last): 1609 1609 ... 1610 ValidationError: [ u'Ensure this value has at most 20 characters.']1610 ValidationError: ['Ensure this value has at most 20 characters.'] 1611 1611 >>> f.clean('not an e-mail') 1612 1612 Traceback (most recent call last): 1613 1613 ... 1614 ValidationError: [ u'Enter a valid e-mail address.']1614 ValidationError: ['Enter a valid e-mail address.'] 1615 1615 >>> f.clean('') 1616 1616 Traceback (most recent call last): 1617 1617 ... 1618 ValidationError: [ u'This field is required.']1618 ValidationError: ['This field is required.'] 1619 1619 >>> f.clean(None) 1620 1620 Traceback (most recent call last): 1621 1621 ... 1622 ValidationError: [ u'This field is required.']1622 ValidationError: ['This field is required.'] 1623 1623 1624 1624 >>> f = ComboField(fields=[CharField(max_length=20), EmailField()], required=False) 1625 1625 >>> f.clean('test@example.com') … … 1627 1627 >>> f.clean('longemailaddress@example.com') 1628 1628 Traceback (most recent call last): 1629 1629 ... 1630 ValidationError: [ u'Ensure this value has at most 20 characters.']1630 ValidationError: ['Ensure this value has at most 20 characters.'] 1631 1631 >>> f.clean('not an e-mail') 1632 1632 Traceback (most recent call last): 1633 1633 ... 1634 ValidationError: [ u'Enter a valid e-mail address.']1634 ValidationError: ['Enter a valid e-mail address.'] 1635 1635 >>> f.clean('') 1636 1636 u'' 1637 1637 >>> f.clean(None) … … 1645 1645 >>> f.clean(None) 1646 1646 Traceback (most recent call last): 1647 1647 ... 1648 ValidationError: [ u'This field is required.']1648 ValidationError: ['This field is required.'] 1649 1649 >>> f.clean('') 1650 1650 Traceback (most recent call last): 1651 1651 ... 1652 ValidationError: [ u'This field is required.']1652 ValidationError: ['This field is required.'] 1653 1653 >>> f.clean('hello') 1654 1654 Traceback (most recent call last): 1655 1655 ... 1656 ValidationError: [ u'Enter a list of values.']1656 ValidationError: ['Enter a list of values.'] 1657 1657 >>> f.clean(['hello', 'there']) 1658 1658 Traceback (most recent call last): 1659 1659 ... 1660 ValidationError: [ u'Enter a valid date.', u'Enter a valid time.']1660 ValidationError: ['Enter a valid date.', 'Enter a valid time.'] 1661 1661 >>> f.clean(['2006-01-10', 'there']) 1662 1662 Traceback (most recent call last): 1663 1663 ... 1664 ValidationError: [ u'Enter a valid time.']1664 ValidationError: ['Enter a valid time.'] 1665 1665 >>> f.clean(['hello', '07:30']) 1666 1666 Traceback (most recent call last): 1667 1667 ... 1668 ValidationError: [ u'Enter a valid date.']1668 ValidationError: ['Enter a valid date.'] 1669 1669 1670 1670 >>> f = SplitDateTimeField(required=False) 1671 1671 >>> f.clean([datetime.date(2006, 1, 10), datetime.time(7, 30)]) … … 1675 1675 >>> f.clean('hello') 1676 1676 Traceback (most recent call last): 1677 1677 ... 1678 ValidationError: [ u'Enter a list of values.']1678 ValidationError: ['Enter a list of values.'] 1679 1679 >>> f.clean(['hello', 'there']) 1680 1680 Traceback (most recent call last): 1681 1681 ... 1682 ValidationError: [ u'Enter a valid date.', u'Enter a valid time.']1682 ValidationError: ['Enter a valid date.', 'Enter a valid time.'] 1683 1683 >>> f.clean(['2006-01-10', 'there']) 1684 1684 Traceback (most recent call last): 1685 1685 ... 1686 ValidationError: [ u'Enter a valid time.']1686 ValidationError: ['Enter a valid time.'] 1687 1687 >>> f.clean(['hello', '07:30']) 1688 1688 Traceback (most recent call last): 1689 1689 ... 1690 ValidationError: [ u'Enter a valid date.']1690 ValidationError: ['Enter a valid date.'] 1691 1691 1692 1692 ######### 1693 1693 # Forms # … … 1749 1749 >>> p.is_bound 1750 1750 True 1751 1751 >>> 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.']} 1753 1753 >>> p.is_valid() 1754 1754 False 1755 1755 >>> p.clean_data … … 1818 1818 1819 1819 >>> p = Person({'last_name': u'Lennon'}) 1820 1820 >>> 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.']} 1822 1822 >>> p.is_valid() 1823 1823 False 1824 1824 >>> p.errors.as_ul() … … 1833 1833 ... 1834 1834 AttributeError: 'Person' object has no attribute 'clean_data' 1835 1835 >>> p['first_name'].errors 1836 [ u'This field is required.']1836 ['This field is required.'] 1837 1837 >>> p['first_name'].errors.as_ul() 1838 1838 u'<ul class="errorlist"><li>This field is required.</li></ul>' 1839 1839 >>> p['first_name'].errors.as_text() … … 2189 2189 returns a list of input. 2190 2190 >>> f = SongForm({'name': 'Yesterday'}, auto_id=False) 2191 2191 >>> f.errors 2192 {'composers': [ u'This field is required.']}2192 {'composers': ['This field is required.']} 2193 2193 >>> f = SongForm({'name': 'Yesterday', 'composers': ['J']}, auto_id=False) 2194 2194 >>> f.errors 2195 2195 {} … … 2223 2223 {} 2224 2224 >>> f = UserRegistration({}, auto_id=False) 2225 2225 >>> 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.']} 2227 2227 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) 2228 2228 >>> f.errors 2229 {'password2': [ u'Please make sure your passwords match.']}2229 {'password2': ['Please make sure your passwords match.']} 2230 2230 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False) 2231 2231 >>> f.errors 2232 2232 {} … … 2257 2257 <tr><th>Password1:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password1" /></td></tr> 2258 2258 <tr><th>Password2:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="password" name="password2" /></td></tr> 2259 2259 >>> 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.']} 2261 2261 >>> f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'bar'}, auto_id=False) 2262 2262 >>> f.errors 2263 {'__all__': [ u'Please make sure your passwords match.']}2263 {'__all__': ['Please make sure your passwords match.']} 2264 2264 >>> print f.as_table() 2265 2265 <tr><td colspan="2"><ul class="errorlist"><li>Please make sure your passwords match.</li></ul></td></tr> 2266 2266 <tr><th>Username:</th><td><input type="text" name="username" value="adrian" maxlength="10" /></td></tr> … … 2551 2551 validation error rather than using the initial value for 'username'. 2552 2552 >>> p = UserRegistration({'password': 'secret'}) 2553 2553 >>> p.errors 2554 {'username': [ u'This field is required.']}2554 {'username': ['This field is required.']} 2555 2555 >>> p.is_valid() 2556 2556 False 2557 2557 … … 2596 2596 validation error rather than using the initial value for 'username'. 2597 2597 >>> p = UserRegistration({'password': 'secret'}, initial={'username': 'django'}) 2598 2598 >>> p.errors 2599 {'username': [ u'This field is required.']}2599 {'username': ['This field is required.']} 2600 2600 >>> p.is_valid() 2601 2601 False 2602 2602 … … 2655 2655 ... } 2656 2656 >>> p = Person(data, prefix='person1') 2657 2657 >>> 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.']} 2659 2659 >>> p['first_name'].errors 2660 [ u'This field is required.']2660 ['This field is required.'] 2661 2661 >>> p['person1-first_name'].errors 2662 2662 Traceback (most recent call last): 2663 2663 ... … … 2672 2672 ... } 2673 2673 >>> p = Person(data, prefix='person1') 2674 2674 >>> 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.']} 2676 2676 2677 2677 With prefixes, a single data dictionary can hold data for multiple instances 2678 2678 of the same form.