2006-11-16 14:45:29 +08:00
# -*- coding: utf-8 -*-
r """
2006-10-29 04:34:37 +08:00
>> > from django . newforms import *
>> > import datetime
>> > import re
2006-12-01 01:48:54 +08:00
###########
# Widgets #
###########
Each Widget class corresponds to an HTML form widget . A Widget knows how to
render itself , given a field name and some data . Widgets don ' t perform
validation .
2006-10-29 04:34:37 +08:00
# TextInput Widget ############################################################
>> > w = TextInput ( )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " text " name= " email " /> '
>> > w . render ( ' email ' , None )
u ' <input type= " text " name= " email " /> '
>> > w . render ( ' email ' , ' test@example.com ' )
u ' <input type= " text " name= " email " value= " test@example.com " /> '
>> > w . render ( ' email ' , ' some " quoted " & ampersanded value ' )
u ' <input type= " text " name= " email " value= " some "quoted" & ampersanded value " /> '
>> > w . render ( ' email ' , ' test@example.com ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " text " name= " email " value= " test@example.com " class= " fun " /> '
2006-11-16 14:45:29 +08:00
# Note that doctest in Python 2.4 (and maybe 2.5?) doesn't support non-ascii
# characters in output, so we're displaying the repr() here.
>> > w . render ( ' email ' , ' ŠĐĆŽćžšđ ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " text " name= " email " value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " class= " fun " /> '
2006-10-29 04:34:37 +08:00
You can also pass ' attrs ' to the constructor :
>> > w = TextInput ( attrs = { ' class ' : ' fun ' } )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " text " class= " fun " name= " email " /> '
>> > w . render ( ' email ' , ' foo@example.com ' )
u ' <input type= " text " class= " fun " value= " foo@example.com " name= " email " /> '
' attrs ' passed to render ( ) get precedence over those passed to the constructor :
>> > w = TextInput ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' email ' , ' ' , attrs = { ' class ' : ' special ' } )
u ' <input type= " text " class= " special " name= " email " /> '
2006-11-02 07:54:17 +08:00
# PasswordInput Widget ############################################################
>> > w = PasswordInput ( )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " password " name= " email " /> '
>> > w . render ( ' email ' , None )
u ' <input type= " password " name= " email " /> '
>> > w . render ( ' email ' , ' test@example.com ' )
u ' <input type= " password " name= " email " value= " test@example.com " /> '
>> > w . render ( ' email ' , ' some " quoted " & ampersanded value ' )
u ' <input type= " password " name= " email " value= " some "quoted" & ampersanded value " /> '
>> > w . render ( ' email ' , ' test@example.com ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " password " name= " email " value= " test@example.com " class= " fun " /> '
You can also pass ' attrs ' to the constructor :
>> > w = PasswordInput ( attrs = { ' class ' : ' fun ' } )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " password " class= " fun " name= " email " /> '
>> > w . render ( ' email ' , ' foo@example.com ' )
u ' <input type= " password " class= " fun " value= " foo@example.com " name= " email " /> '
' attrs ' passed to render ( ) get precedence over those passed to the constructor :
>> > w = PasswordInput ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' email ' , ' ' , attrs = { ' class ' : ' special ' } )
u ' <input type= " password " class= " special " name= " email " /> '
2006-11-16 14:45:29 +08:00
>> > w . render ( ' email ' , ' ŠĐĆŽćžšđ ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " password " class= " fun " value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " name= " email " /> '
2006-11-02 07:54:17 +08:00
# HiddenInput Widget ############################################################
>> > w = HiddenInput ( )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " hidden " name= " email " /> '
>> > w . render ( ' email ' , None )
u ' <input type= " hidden " name= " email " /> '
>> > w . render ( ' email ' , ' test@example.com ' )
u ' <input type= " hidden " name= " email " value= " test@example.com " /> '
>> > w . render ( ' email ' , ' some " quoted " & ampersanded value ' )
u ' <input type= " hidden " name= " email " value= " some "quoted" & ampersanded value " /> '
>> > w . render ( ' email ' , ' test@example.com ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " hidden " name= " email " value= " test@example.com " class= " fun " /> '
You can also pass ' attrs ' to the constructor :
>> > w = HiddenInput ( attrs = { ' class ' : ' fun ' } )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " hidden " class= " fun " name= " email " /> '
>> > w . render ( ' email ' , ' foo@example.com ' )
u ' <input type= " hidden " class= " fun " value= " foo@example.com " name= " email " /> '
' attrs ' passed to render ( ) get precedence over those passed to the constructor :
>> > w = HiddenInput ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' email ' , ' ' , attrs = { ' class ' : ' special ' } )
u ' <input type= " hidden " class= " special " name= " email " /> '
2006-11-16 14:45:29 +08:00
>> > w . render ( ' email ' , ' ŠĐĆŽćžšđ ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " hidden " class= " fun " value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " name= " email " /> '
' attrs ' passed to render ( ) get precedence over those passed to the constructor :
>> > w = HiddenInput ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' email ' , ' ' , attrs = { ' class ' : ' special ' } )
u ' <input type= " hidden " class= " special " name= " email " /> '
2006-11-02 07:54:17 +08:00
# FileInput Widget ############################################################
>> > w = FileInput ( )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " file " name= " email " /> '
>> > w . render ( ' email ' , None )
u ' <input type= " file " name= " email " /> '
>> > w . render ( ' email ' , ' test@example.com ' )
u ' <input type= " file " name= " email " value= " test@example.com " /> '
>> > w . render ( ' email ' , ' some " quoted " & ampersanded value ' )
u ' <input type= " file " name= " email " value= " some "quoted" & ampersanded value " /> '
>> > w . render ( ' email ' , ' test@example.com ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " file " name= " email " value= " test@example.com " class= " fun " /> '
You can also pass ' attrs ' to the constructor :
>> > w = FileInput ( attrs = { ' class ' : ' fun ' } )
>> > w . render ( ' email ' , ' ' )
u ' <input type= " file " class= " fun " name= " email " /> '
>> > w . render ( ' email ' , ' foo@example.com ' )
u ' <input type= " file " class= " fun " value= " foo@example.com " name= " email " /> '
2006-11-16 14:45:29 +08:00
>> > w . render ( ' email ' , ' ŠĐĆŽćžšđ ' , attrs = { ' class ' : ' fun ' } )
u ' <input type= " file " class= " fun " value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " name= " email " /> '
2006-11-02 07:54:17 +08:00
2006-10-29 04:34:37 +08:00
# Textarea Widget #############################################################
>> > w = Textarea ( )
>> > w . render ( ' msg ' , ' ' )
u ' <textarea name= " msg " ></textarea> '
>> > w . render ( ' msg ' , None )
u ' <textarea name= " msg " ></textarea> '
>> > w . render ( ' msg ' , ' value ' )
u ' <textarea name= " msg " >value</textarea> '
>> > w . render ( ' msg ' , ' some " quoted " & ampersanded value ' )
u ' <textarea name= " msg " >some "quoted" & ampersanded value</textarea> '
>> > w . render ( ' msg ' , ' value ' , attrs = { ' class ' : ' pretty ' } )
u ' <textarea name= " msg " class= " pretty " >value</textarea> '
You can also pass ' attrs ' to the constructor :
>> > w = Textarea ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' msg ' , ' ' )
u ' <textarea class= " pretty " name= " msg " ></textarea> '
>> > w . render ( ' msg ' , ' example ' )
u ' <textarea class= " pretty " name= " msg " >example</textarea> '
' attrs ' passed to render ( ) get precedence over those passed to the constructor :
>> > w = Textarea ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' msg ' , ' ' , attrs = { ' class ' : ' special ' } )
u ' <textarea class= " special " name= " msg " ></textarea> '
2006-11-16 14:45:29 +08:00
>> > w . render ( ' msg ' , ' ŠĐĆŽćžšđ ' , attrs = { ' class ' : ' fun ' } )
u ' <textarea class= " fun " name= " msg " > \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 </textarea> '
2006-10-29 04:34:37 +08:00
# CheckboxInput Widget ########################################################
>> > w = CheckboxInput ( )
>> > w . render ( ' is_cool ' , ' ' )
u ' <input type= " checkbox " name= " is_cool " /> '
>> > w . render ( ' is_cool ' , False )
u ' <input type= " checkbox " name= " is_cool " /> '
>> > w . render ( ' is_cool ' , True )
u ' <input checked= " checked " type= " checkbox " name= " is_cool " /> '
>> > w . render ( ' is_cool ' , False , attrs = { ' class ' : ' pretty ' } )
u ' <input type= " checkbox " name= " is_cool " class= " pretty " /> '
You can also pass ' attrs ' to the constructor :
>> > w = CheckboxInput ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' is_cool ' , ' ' )
u ' <input type= " checkbox " class= " pretty " name= " is_cool " /> '
' attrs ' passed to render ( ) get precedence over those passed to the constructor :
>> > w = CheckboxInput ( attrs = { ' class ' : ' pretty ' } )
>> > w . render ( ' is_cool ' , ' ' , attrs = { ' class ' : ' special ' } )
u ' <input type= " checkbox " class= " special " name= " is_cool " /> '
2006-11-02 07:54:17 +08:00
# Select Widget ###############################################################
>> > w = Select ( )
>> > print w . render ( ' beatle ' , ' J ' , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select name = " beatle " >
< option value = " J " selected = " selected " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
If the value is None , none of the options are selected :
>> > print w . render ( ' beatle ' , None , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select name = " beatle " >
< option value = " J " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
If the value corresponds to a label ( but not to an option value ) , none of the options are selected :
>> > print w . render ( ' beatle ' , ' John ' , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select name = " beatle " >
< option value = " J " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
The value is compared to its str ( ) :
>> > print w . render ( ' num ' , 2 , choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) , ( ' 3 ' , ' 3 ' ) ] )
< select name = " num " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
>> > print w . render ( ' num ' , ' 2 ' , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< select name = " num " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
>> > print w . render ( ' num ' , 2 , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< select name = " num " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
The ' choices ' argument can be any iterable :
>> > def get_choices ( ) :
. . . for i in range ( 5 ) :
. . . yield ( i , i )
>> > print w . render ( ' num ' , 2 , choices = get_choices ( ) )
< select name = " num " >
< option value = " 0 " > 0 < / option >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< option value = " 4 " > 4 < / option >
< / select >
You can also pass ' choices ' to the constructor :
>> > w = Select ( choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
>> > print w . render ( ' num ' , 2 )
< select name = " num " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
If ' choices ' is passed to both the constructor and render ( ) , then they ' ll both be in the output:
>> > print w . render ( ' num ' , 2 , choices = [ ( 4 , 4 ) , ( 5 , 5 ) ] )
< select name = " num " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< option value = " 4 " > 4 < / option >
< option value = " 5 " > 5 < / option >
< / select >
2006-11-16 14:45:29 +08:00
>> > w . render ( ' email ' , ' ŠĐĆŽćžšđ ' , choices = [ ( ' ŠĐĆŽćžšđ ' , ' ŠĐabcĆŽćžšđ ' ) , ( ' ćžšđ ' , ' abcćžšđ ' ) ] )
u ' <select name= " email " > \n <option value= " 1 " >1</option> \n <option value= " 2 " >2</option> \n <option value= " 3 " >3</option> \n <option value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " selected= " selected " > \u0160 \u0110 abc \u0106 \u017d \u0107 \u017e \u0161 \u0111 </option> \n <option value= " \u0107 \u017e \u0161 \u0111 " >abc \u0107 \u017e \u0161 \u0111 </option> \n </select> '
2006-11-02 09:06:12 +08:00
# SelectMultiple Widget #######################################################
>> > w = SelectMultiple ( )
>> > print w . render ( ' beatles ' , [ ' J ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select multiple = " multiple " name = " beatles " >
< option value = " J " selected = " selected " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
>> > print w . render ( ' beatles ' , [ ' J ' , ' P ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select multiple = " multiple " name = " beatles " >
< option value = " J " selected = " selected " > John < / option >
< option value = " P " selected = " selected " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
>> > print w . render ( ' beatles ' , [ ' J ' , ' P ' , ' R ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select multiple = " multiple " name = " beatles " >
< option value = " J " selected = " selected " > John < / option >
< option value = " P " selected = " selected " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " selected = " selected " > Ringo < / option >
< / select >
If the value is None , none of the options are selected :
>> > print w . render ( ' beatles ' , None , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select multiple = " multiple " name = " beatles " >
< option value = " J " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
If the value corresponds to a label ( but not to an option value ) , none of the options are selected :
>> > print w . render ( ' beatles ' , [ ' John ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select multiple = " multiple " name = " beatles " >
< option value = " J " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
If multiple values are given , but some of them are not valid , the valid ones are selected :
>> > print w . render ( ' beatles ' , [ ' J ' , ' G ' , ' foo ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< select multiple = " multiple " name = " beatles " >
< option value = " J " selected = " selected " > John < / option >
< option value = " P " > Paul < / option >
< option value = " G " selected = " selected " > George < / option >
< option value = " R " > Ringo < / option >
< / select >
The value is compared to its str ( ) :
>> > print w . render ( ' nums ' , [ 2 ] , choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) , ( ' 3 ' , ' 3 ' ) ] )
< select multiple = " multiple " name = " nums " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
>> > print w . render ( ' nums ' , [ ' 2 ' ] , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< select multiple = " multiple " name = " nums " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
>> > print w . render ( ' nums ' , [ 2 ] , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< select multiple = " multiple " name = " nums " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
The ' choices ' argument can be any iterable :
>> > def get_choices ( ) :
. . . for i in range ( 5 ) :
. . . yield ( i , i )
>> > print w . render ( ' nums ' , [ 2 ] , choices = get_choices ( ) )
< select multiple = " multiple " name = " nums " >
< option value = " 0 " > 0 < / option >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< option value = " 4 " > 4 < / option >
< / select >
You can also pass ' choices ' to the constructor :
>> > w = SelectMultiple ( choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
>> > print w . render ( ' nums ' , [ 2 ] )
< select multiple = " multiple " name = " nums " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< / select >
If ' choices ' is passed to both the constructor and render ( ) , then they ' ll both be in the output:
>> > print w . render ( ' nums ' , [ 2 ] , choices = [ ( 4 , 4 ) , ( 5 , 5 ) ] )
< select multiple = " multiple " name = " nums " >
< option value = " 1 " > 1 < / option >
< option value = " 2 " selected = " selected " > 2 < / option >
< option value = " 3 " > 3 < / option >
< option value = " 4 " > 4 < / option >
< option value = " 5 " > 5 < / option >
< / select >
2006-11-16 14:45:29 +08:00
>> > w . render ( ' nums ' , [ ' ŠĐĆŽćžšđ ' ] , choices = [ ( ' ŠĐĆŽćžšđ ' , ' ŠĐabcĆŽćžšđ ' ) , ( ' ćžšđ ' , ' abcćžšđ ' ) ] )
u ' <select multiple= " multiple " name= " nums " > \n <option value= " 1 " >1</option> \n <option value= " 2 " >2</option> \n <option value= " 3 " >3</option> \n <option value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " selected= " selected " > \u0160 \u0110 abc \u0106 \u017d \u0107 \u017e \u0161 \u0111 </option> \n <option value= " \u0107 \u017e \u0161 \u0111 " >abc \u0107 \u017e \u0161 \u0111 </option> \n </select> '
2006-11-16 06:08:22 +08:00
# RadioSelect Widget ##########################################################
>> > w = RadioSelect ( )
>> > print w . render ( ' beatle ' , ' J ' , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input checked = " checked " type = " radio " name = " beatle " value = " J " / > John < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " P " / > Paul < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " G " / > George < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " R " / > Ringo < / label > < / li >
< / ul >
If the value is None , none of the options are checked :
>> > print w . render ( ' beatle ' , None , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input type = " radio " name = " beatle " value = " J " / > John < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " P " / > Paul < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " G " / > George < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " R " / > Ringo < / label > < / li >
< / ul >
If the value corresponds to a label ( but not to an option value ) , none of the options are checked :
>> > print w . render ( ' beatle ' , ' John ' , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input type = " radio " name = " beatle " value = " J " / > John < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " P " / > Paul < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " G " / > George < / label > < / li >
< li > < label > < input type = " radio " name = " beatle " value = " R " / > Ringo < / label > < / li >
< / ul >
The value is compared to its str ( ) :
>> > print w . render ( ' num ' , 2 , choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) , ( ' 3 ' , ' 3 ' ) ] )
< ul >
< li > < label > < input type = " radio " name = " num " value = " 1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " radio " name = " num " value = " 2 " / > 2 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 3 " / > 3 < / label > < / li >
< / ul >
>> > print w . render ( ' num ' , ' 2 ' , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< ul >
< li > < label > < input type = " radio " name = " num " value = " 1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " radio " name = " num " value = " 2 " / > 2 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 3 " / > 3 < / label > < / li >
< / ul >
>> > print w . render ( ' num ' , 2 , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< ul >
< li > < label > < input type = " radio " name = " num " value = " 1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " radio " name = " num " value = " 2 " / > 2 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 3 " / > 3 < / label > < / li >
< / ul >
The ' choices ' argument can be any iterable :
>> > def get_choices ( ) :
. . . for i in range ( 5 ) :
. . . yield ( i , i )
>> > print w . render ( ' num ' , 2 , choices = get_choices ( ) )
< ul >
< li > < label > < input type = " radio " name = " num " value = " 0 " / > 0 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " radio " name = " num " value = " 2 " / > 2 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 3 " / > 3 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 4 " / > 4 < / label > < / li >
< / ul >
You can also pass ' choices ' to the constructor :
>> > w = RadioSelect ( choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
>> > print w . render ( ' num ' , 2 )
< ul >
< li > < label > < input type = " radio " name = " num " value = " 1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " radio " name = " num " value = " 2 " / > 2 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 3 " / > 3 < / label > < / li >
< / ul >
If ' choices ' is passed to both the constructor and render ( ) , then they ' ll both be in the output:
>> > print w . render ( ' num ' , 2 , choices = [ ( 4 , 4 ) , ( 5 , 5 ) ] )
< ul >
< li > < label > < input type = " radio " name = " num " value = " 1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " radio " name = " num " value = " 2 " / > 2 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 3 " / > 3 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 4 " / > 4 < / label > < / li >
< li > < label > < input type = " radio " name = " num " value = " 5 " / > 5 < / label > < / li >
< / ul >
The render ( ) method returns a RadioFieldRenderer object , whose str ( ) is a < ul > .
You can manipulate that object directly to customize the way the RadioSelect
is rendered .
>> > w = RadioSelect ( )
>> > r = w . render ( ' beatle ' , ' J ' , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
>> > for inp in r :
. . . print inp
< label > < input checked = " checked " type = " radio " name = " beatle " value = " J " / > John < / label >
< label > < input type = " radio " name = " beatle " value = " P " / > Paul < / label >
< label > < input type = " radio " name = " beatle " value = " G " / > George < / label >
< label > < input type = " radio " name = " beatle " value = " R " / > Ringo < / label >
>> > for inp in r :
. . . print ' %s <br /> ' % inp
< label > < input checked = " checked " type = " radio " name = " beatle " value = " J " / > John < / label > < br / >
< label > < input type = " radio " name = " beatle " value = " P " / > Paul < / label > < br / >
< label > < input type = " radio " name = " beatle " value = " G " / > George < / label > < br / >
< label > < input type = " radio " name = " beatle " value = " R " / > Ringo < / label > < br / >
>> > for inp in r :
. . . print ' <p> %s %s </p> ' % ( inp . tag ( ) , inp . choice_label )
< p > < input checked = " checked " type = " radio " name = " beatle " value = " J " / > John < / p >
< p > < input type = " radio " name = " beatle " value = " P " / > Paul < / p >
< p > < input type = " radio " name = " beatle " value = " G " / > George < / p >
< p > < input type = " radio " name = " beatle " value = " R " / > Ringo < / p >
>> > for inp in r :
. . . print ' %s %s %s %s %s ' % ( inp . name , inp . value , inp . choice_value , inp . choice_label , inp . is_checked ( ) )
beatle J J John True
beatle J P Paul False
beatle J G George False
beatle J R Ringo False
2006-11-29 11:02:26 +08:00
# CheckboxSelectMultiple Widget ###############################################
>> > w = CheckboxSelectMultiple ( )
>> > print w . render ( ' beatles ' , [ ' J ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesJ " / > John < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesP " / > Paul < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesG " / > George < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesR " / > Ringo < / label > < / li >
< / ul >
>> > print w . render ( ' beatles ' , [ ' J ' , ' P ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesJ " / > John < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesP " / > Paul < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesG " / > George < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesR " / > Ringo < / label > < / li >
< / ul >
>> > print w . render ( ' beatles ' , [ ' J ' , ' P ' , ' R ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesJ " / > John < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesP " / > Paul < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesG " / > George < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesR " / > Ringo < / label > < / li >
< / ul >
If the value is None , none of the options are selected :
>> > print w . render ( ' beatles ' , None , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input type = " checkbox " name = " beatlesJ " / > John < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesP " / > Paul < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesG " / > George < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesR " / > Ringo < / label > < / li >
< / ul >
If the value corresponds to a label ( but not to an option value ) , none of the options are selected :
>> > print w . render ( ' beatles ' , [ ' John ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input type = " checkbox " name = " beatlesJ " / > John < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesP " / > Paul < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesG " / > George < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesR " / > Ringo < / label > < / li >
< / ul >
If multiple values are given , but some of them are not valid , the valid ones are selected :
>> > print w . render ( ' beatles ' , [ ' J ' , ' G ' , ' foo ' ] , choices = ( ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) , ( ' G ' , ' George ' ) , ( ' R ' , ' Ringo ' ) ) )
< ul >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesJ " / > John < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesP " / > Paul < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " beatlesG " / > George < / label > < / li >
< li > < label > < input type = " checkbox " name = " beatlesR " / > Ringo < / label > < / li >
< / ul >
The value is compared to its str ( ) :
>> > print w . render ( ' nums ' , [ 2 ] , choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) , ( ' 3 ' , ' 3 ' ) ] )
< ul >
< li > < label > < input type = " checkbox " name = " nums1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " nums2 " / > 2 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums3 " / > 3 < / label > < / li >
< / ul >
>> > print w . render ( ' nums ' , [ ' 2 ' ] , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< ul >
< li > < label > < input type = " checkbox " name = " nums1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " nums2 " / > 2 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums3 " / > 3 < / label > < / li >
< / ul >
>> > print w . render ( ' nums ' , [ 2 ] , choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
< ul >
< li > < label > < input type = " checkbox " name = " nums1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " nums2 " / > 2 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums3 " / > 3 < / label > < / li >
< / ul >
The ' choices ' argument can be any iterable :
>> > def get_choices ( ) :
. . . for i in range ( 5 ) :
. . . yield ( i , i )
>> > print w . render ( ' nums ' , [ 2 ] , choices = get_choices ( ) )
< ul >
< li > < label > < input type = " checkbox " name = " nums0 " / > 0 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " nums2 " / > 2 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums3 " / > 3 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums4 " / > 4 < / label > < / li >
< / ul >
You can also pass ' choices ' to the constructor :
>> > w = CheckboxSelectMultiple ( choices = [ ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) ] )
>> > print w . render ( ' nums ' , [ 2 ] )
< ul >
< li > < label > < input type = " checkbox " name = " nums1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " nums2 " / > 2 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums3 " / > 3 < / label > < / li >
< / ul >
If ' choices ' is passed to both the constructor and render ( ) , then they ' ll both be in the output:
>> > print w . render ( ' nums ' , [ 2 ] , choices = [ ( 4 , 4 ) , ( 5 , 5 ) ] )
< ul >
< li > < label > < input type = " checkbox " name = " nums1 " / > 1 < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " nums2 " / > 2 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums3 " / > 3 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums4 " / > 4 < / label > < / li >
< li > < label > < input type = " checkbox " name = " nums5 " / > 5 < / label > < / li >
< / ul >
>> > w . render ( ' nums ' , [ ' ŠĐĆŽćžšđ ' ] , choices = [ ( ' ŠĐĆŽćžšđ ' , ' ŠĐabcĆŽćžšđ ' ) , ( ' ćžšđ ' , ' abcćžšđ ' ) ] )
u ' <ul> \n <li><label><input type= " checkbox " name= " nums1 " /> 1</label></li> \n <li><label><input type= " checkbox " name= " nums2 " /> 2</label></li> \n <li><label><input type= " checkbox " name= " nums3 " /> 3</label></li> \n <li><label><input checked= " checked " type= " checkbox " name= " nums \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " /> \u0160 \u0110 abc \u0106 \u017d \u0107 \u017e \u0161 \u0111 </label></li> \n <li><label><input type= " checkbox " name= " nums \u0107 \u017e \u0161 \u0111 " /> abc \u0107 \u017e \u0161 \u0111 </label></li> \n </ul> '
2006-12-01 01:48:54 +08:00
##########
# Fields #
##########
Each Field class does some sort of validation . Each Field has a clean ( ) method ,
which either raises django . newforms . ValidationError or returns the " clean "
data - - usually a Unicode object , but , in some rare cases , a list .
Each Field ' s __init__() takes at least these parameters:
required - - Boolean that specifies whether the field is required .
True by default .
widget - - A Widget class , or instance of a Widget class , that should be
used for this Field when displaying it . Each Field has a default
Widget that it ' ll use if you don ' t specify this . In most cases ,
the default widget is TextInput .
Other than that , the Field subclasses have class - specific options for
__init__ ( ) . For example , CharField has a max_length option .
2006-10-29 04:34:37 +08:00
# CharField ###################################################################
2006-11-27 08:49:26 +08:00
>> > f = CharField ( )
>> > f . clean ( 1 )
u ' 1 '
>> > f . clean ( ' hello ' )
u ' hello '
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( [ 1 , 2 , 3 ] )
u ' [1, 2, 3] '
2006-10-29 04:34:37 +08:00
>> > f = CharField ( required = False )
2006-11-05 04:49:59 +08:00
>> > f . clean ( 1 )
2006-10-29 04:34:37 +08:00
u ' 1 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' hello ' )
2006-10-29 04:34:37 +08:00
u ' hello '
2006-11-05 04:49:59 +08:00
>> > f . clean ( None )
2006-10-29 04:34:37 +08:00
u ' '
2006-11-27 08:23:17 +08:00
>> > f . clean ( ' ' )
u ' '
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ 1 , 2 , 3 ] )
2006-10-29 04:34:37 +08:00
u ' [1, 2, 3] '
CharField accepts an optional max_length parameter :
>> > f = CharField ( max_length = 10 , required = False )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 12345 ' )
2006-10-29 04:34:37 +08:00
u ' 12345 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1234567890 ' )
2006-10-29 04:34:37 +08:00
u ' 1234567890 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1234567890a ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Ensure this value has at most 10 characters. ' ]
CharField accepts an optional min_length parameter :
>> > f = CharField ( min_length = 10 , required = False )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Ensure this value has at least 10 characters. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 12345 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Ensure this value has at least 10 characters. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1234567890 ' )
2006-10-29 04:34:37 +08:00
u ' 1234567890 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1234567890a ' )
2006-10-29 04:34:37 +08:00
u ' 1234567890a '
# IntegerField ################################################################
>> > f = IntegerField ( )
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( ' 1 ' )
1
>> > isinstance ( f . clean ( ' 1 ' ) , int )
True
>> > f . clean ( ' 23 ' )
23
>> > f . clean ( ' a ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a whole number. ' ]
>> > f . clean ( ' 1 ' )
1
>> > f . clean ( ' 1 ' )
1
>> > f . clean ( ' 1 ' )
1
>> > f . clean ( ' 1a ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a whole number. ' ]
>> > f = IntegerField ( required = False )
>> > f . clean ( ' ' )
u ' '
>> > f . clean ( None )
u ' '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1 ' )
2006-10-29 04:34:37 +08:00
1
2006-11-05 04:49:59 +08:00
>> > isinstance ( f . clean ( ' 1 ' ) , int )
2006-10-29 04:34:37 +08:00
True
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 23 ' )
2006-10-29 04:34:37 +08:00
23
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' a ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a whole number. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1 ' )
2006-10-29 04:34:37 +08:00
1
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1 ' )
2006-10-29 04:34:37 +08:00
1
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1 ' )
2006-10-29 04:34:37 +08:00
1
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1a ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a whole number. ' ]
# DateField ###################################################################
>> > import datetime
>> > f = DateField ( )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . date ( 2006 , 10 , 25 ) )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 ) )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 ) )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 , 200 ) )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/2006 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/06 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' Oct 25 2006 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' October 25 2006 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' October 25, 2006 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 25 October 2006 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 25 October, 2006 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-4-31 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 200a-10-25 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 25/10/06 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( None )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f = DateField ( required = False )
2006-11-05 04:49:59 +08:00
>> > f . clean ( None )
>> > repr ( f . clean ( None ) )
2006-10-29 04:34:37 +08:00
' None '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' ' )
>> > repr ( f . clean ( ' ' ) )
2006-10-29 04:34:37 +08:00
' None '
DateField accepts an optional input_formats parameter :
>> > f = DateField ( input_formats = [ ' % Y % m %d ' ] )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . date ( 2006 , 10 , 25 ) )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 ) )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006 10 25 ' )
2006-10-29 04:34:37 +08:00
datetime . date ( 2006 , 10 , 25 )
The input_formats parameter overrides all default input formats ,
so the default formats won ' t work unless you specify them:
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/2006 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/06 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date. ' ]
# DateTimeField ###############################################################
>> > import datetime
>> > f = DateTimeField ( )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . date ( 2006 , 10 , 25 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 0 , 0 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 , 200 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 , 200 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 14:30:45 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 45 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 14:30:00 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 14:30 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 0 , 0 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/2006 14:30:45 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 45 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/2006 14:30:00 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/2006 14:30 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/2006 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 0 , 0 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/06 14:30:45 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 45 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/06 14:30:00 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/06 14:30 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 10/25/06 ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 0 , 0 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' hello ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date/time. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 4:30 p.m. ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date/time. ' ]
DateField accepts an optional input_formats parameter :
>> > f = DateTimeField ( input_formats = [ ' % Y % m %d % I: % M % p ' ] )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . date ( 2006 , 10 , 25 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 0 , 0 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 , 200 ) )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 , 59 , 200 )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006 10 25 2:30 PM ' )
2006-10-29 04:34:37 +08:00
datetime . datetime ( 2006 , 10 , 25 , 14 , 30 )
The input_formats parameter overrides all default input formats ,
so the default formats won ' t work unless you specify them:
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2006-10-25 14:30:45 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid date/time. ' ]
2006-11-27 08:49:26 +08:00
>> > f = DateTimeField ( required = False )
>> > f . clean ( None )
>> > repr ( f . clean ( None ) )
' None '
>> > f . clean ( ' ' )
>> > repr ( f . clean ( ' ' ) )
' None '
2006-10-29 04:34:37 +08:00
# RegexField ##################################################################
>> > f = RegexField ( ' ^ \ d[A-F] \ d$ ' )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2A2 ' )
2006-10-29 04:34:37 +08:00
u ' 2A2 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 3F3 ' )
2006-10-29 04:34:37 +08:00
u ' 3F3 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 3G3 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2A2 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2A2 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
2006-11-27 08:23:17 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f = RegexField ( ' ^ \ d[A-F] \ d$ ' , required = False )
>> > f . clean ( ' 2A2 ' )
u ' 2A2 '
>> > f . clean ( ' 3F3 ' )
u ' 3F3 '
>> > f . clean ( ' 3G3 ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
>> > f . clean ( ' ' )
u ' '
2006-10-29 04:34:37 +08:00
Alternatively , RegexField can take a compiled regular expression :
>> > f = RegexField ( re . compile ( ' ^ \ d[A-F] \ d$ ' ) )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2A2 ' )
2006-10-29 04:34:37 +08:00
u ' 2A2 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 3F3 ' )
2006-10-29 04:34:37 +08:00
u ' 3F3 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 3G3 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2A2 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 2A2 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid value. ' ]
RegexField takes an optional error_message argument :
>> > f = RegexField ( ' ^ \ d \ d \ d \ d$ ' , ' Enter a four-digit number. ' )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1234 ' )
2006-10-29 04:34:37 +08:00
u ' 1234 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 123 ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a four-digit number. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' abcd ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a four-digit number. ' ]
# EmailField ##################################################################
>> > f = EmailField ( )
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( ' person@example.com ' )
u ' person@example.com '
>> > f . clean ( ' foo ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
>> > f . clean ( ' foo@ ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
>> > f . clean ( ' foo@bar ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
>> > f = EmailField ( required = False )
>> > f . clean ( ' ' )
u ' '
>> > f . clean ( None )
u ' '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' person@example.com ' )
2006-10-29 04:34:37 +08:00
u ' person@example.com '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' foo ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' foo@ ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' foo@bar ' )
2006-10-29 04:34:37 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
2006-11-02 07:54:17 +08:00
# URLField ##################################################################
>> > f = URLField ( )
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( ' http://example.com ' )
u ' http://example.com '
>> > f . clean ( ' http://www.example.com ' )
u ' http://www.example.com '
>> > f . clean ( ' foo ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
>> > f . clean ( ' example.com ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
>> > f . clean ( ' http:// ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
>> > f . clean ( ' http://example ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
>> > f . clean ( ' http://example. ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
>> > f . clean ( ' http://.com ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
>> > f = URLField ( required = False )
>> > f . clean ( ' ' )
u ' '
>> > f . clean ( None )
u ' '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://example.com ' )
2006-11-02 07:54:17 +08:00
u ' http://example.com '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://www.example.com ' )
2006-11-02 07:54:17 +08:00
u ' http://www.example.com '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' foo ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' example.com ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http:// ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://example ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://example. ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://.com ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
URLField takes an optional verify_exists parameter , which is False by default .
This verifies that the URL is live on the Internet and doesn ' t return a 404 or 500:
>> > f = URLField ( verify_exists = True )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://www.google.com ' ) # This will fail if there's no Internet connection
2006-11-02 07:54:17 +08:00
u ' http://www.google.com '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://example ' )
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid URL. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://www.jfoiwjfoi23jfoijoaijfoiwjofiwjefewl.com ' ) # bad domain
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This URL appears to be a broken link. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' http://google.com/we-love-microsoft.html ' ) # good domain, bad page
2006-11-02 07:54:17 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This URL appears to be a broken link. ' ]
2006-10-29 04:34:37 +08:00
# BooleanField ################################################################
>> > f = BooleanField ( )
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( True )
True
>> > f . clean ( False )
False
>> > f . clean ( 1 )
True
>> > f . clean ( 0 )
False
>> > f . clean ( ' Django rocks ' )
True
>> > f = BooleanField ( required = False )
>> > f . clean ( ' ' )
False
>> > f . clean ( None )
False
2006-11-05 04:49:59 +08:00
>> > f . clean ( True )
2006-10-29 04:34:37 +08:00
True
2006-11-05 04:49:59 +08:00
>> > f . clean ( False )
2006-10-29 04:34:37 +08:00
False
2006-11-05 04:49:59 +08:00
>> > f . clean ( 1 )
2006-10-29 04:34:37 +08:00
True
2006-11-05 04:49:59 +08:00
>> > f . clean ( 0 )
2006-10-29 04:34:37 +08:00
False
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' Django rocks ' )
2006-10-29 04:34:37 +08:00
True
2006-11-02 11:16:12 +08:00
# ChoiceField #################################################################
>> > f = ChoiceField ( choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) ] )
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( 1 )
2006-11-02 11:16:12 +08:00
u ' 1 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 1 ' )
2006-11-02 11:16:12 +08:00
u ' 1 '
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' 3 ' )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
2006-11-27 08:49:26 +08:00
ValidationError : [ u ' Select a valid choice. 3 is not one of the available choices. ' ]
>> > f = ChoiceField ( choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) ] , required = False )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' ' )
2006-11-27 08:49:26 +08:00
u ' '
>> > f . clean ( None )
u ' '
>> > f . clean ( 1 )
u ' 1 '
>> > f . clean ( ' 1 ' )
u ' 1 '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' 3 ' )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Select a valid choice. 3 is not one of the available choices. ' ]
>> > f = ChoiceField ( choices = [ ( ' J ' , ' John ' ) , ( ' P ' , ' Paul ' ) ] )
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' J ' )
2006-11-02 11:16:12 +08:00
u ' J '
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' John ' )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Select a valid choice. John is not one of the available choices. ' ]
# MultipleChoiceField #########################################################
>> > f = MultipleChoiceField ( choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) ] )
2006-11-27 08:49:26 +08:00
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ 1 ] )
2006-11-02 11:16:12 +08:00
[ u ' 1 ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ ' 1 ' ] )
2006-11-02 11:16:12 +08:00
[ u ' 1 ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ ' 1 ' , ' 2 ' ] )
2006-11-02 11:16:12 +08:00
[ u ' 1 ' , u ' 2 ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ 1 , ' 2 ' ] )
2006-11-02 11:16:12 +08:00
[ u ' 1 ' , u ' 2 ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ( 1 , ' 2 ' ) )
2006-11-02 11:16:12 +08:00
[ u ' 1 ' , u ' 2 ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ' hello ' )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a list of values. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ ] )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( ( ) )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
2006-11-05 04:49:59 +08:00
>> > f . clean ( [ ' 3 ' ] )
2006-11-02 11:16:12 +08:00
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Select a valid choice. 3 is not one of the available choices. ' ]
2006-11-27 08:49:26 +08:00
>> > f = MultipleChoiceField ( choices = [ ( ' 1 ' , ' 1 ' ) , ( ' 2 ' , ' 2 ' ) ] , required = False )
>> > f . clean ( ' ' )
[ ]
>> > f . clean ( None )
[ ]
>> > f . clean ( [ 1 ] )
[ u ' 1 ' ]
>> > f . clean ( [ ' 1 ' ] )
[ u ' 1 ' ]
>> > f . clean ( [ ' 1 ' , ' 2 ' ] )
[ u ' 1 ' , u ' 2 ' ]
>> > f . clean ( [ 1 , ' 2 ' ] )
[ u ' 1 ' , u ' 2 ' ]
>> > f . clean ( ( 1 , ' 2 ' ) )
[ u ' 1 ' , u ' 2 ' ]
>> > f . clean ( ' hello ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a list of values. ' ]
>> > f . clean ( [ ] )
[ ]
>> > f . clean ( ( ) )
[ ]
>> > f . clean ( [ ' 3 ' ] )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Select a valid choice. 3 is not one of the available choices. ' ]
2006-11-05 04:49:59 +08:00
# ComboField ##################################################################
ComboField takes a list of fields that should be used to validate a value ,
2006-11-27 08:49:26 +08:00
in that order .
2006-11-05 04:49:59 +08:00
>> > f = ComboField ( fields = [ CharField ( max_length = 20 ) , EmailField ( ) ] )
>> > f . clean ( ' test@example.com ' )
u ' test@example.com '
>> > f . clean ( ' longemailaddress@example.com ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Ensure this value has at most 20 characters. ' ]
>> > f . clean ( ' not an e-mail ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
>> > f . clean ( ' ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
>> > f . clean ( None )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' This field is required. ' ]
2006-11-27 08:49:26 +08:00
>> > f = ComboField ( fields = [ CharField ( max_length = 20 ) , EmailField ( ) ] , required = False )
>> > f . clean ( ' test@example.com ' )
u ' test@example.com '
>> > f . clean ( ' longemailaddress@example.com ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Ensure this value has at most 20 characters. ' ]
>> > f . clean ( ' not an e-mail ' )
Traceback ( most recent call last ) :
. . .
ValidationError : [ u ' Enter a valid e-mail address. ' ]
>> > f . clean ( ' ' )
u ' '
>> > f . clean ( None )
u ' '
2006-12-01 01:48:54 +08:00
#########
# Forms #
#########
A Form is a collection of Fields . It knows how to validate a set of data and it
knows how to render itself in a couple of default ways ( e . g . , an HTML table ) .
You can pass it data in __init__ ( ) , as a dictionary .
2006-10-29 04:34:37 +08:00
# Form ########################################################################
>> > class Person ( Form ) :
. . . first_name = CharField ( )
. . . last_name = CharField ( )
. . . birthday = DateField ( )
2006-11-05 04:49:59 +08:00
2006-11-27 11:49:19 +08:00
Pass a dictionary to a Form ' s __init__().
2006-10-29 04:34:37 +08:00
>> > p = Person ( { ' first_name ' : u ' John ' , ' last_name ' : u ' Lennon ' , ' birthday ' : u ' 1940-10-9 ' } )
2006-11-27 09:55:24 +08:00
>> > p . errors
2006-10-29 04:34:37 +08:00
{ }
>> > p . is_valid ( )
True
2006-11-27 09:55:24 +08:00
>> > p . errors . as_ul ( )
2006-10-29 04:34:37 +08:00
u ' '
2006-11-27 09:55:24 +08:00
>> > p . errors . as_text ( )
2006-10-29 04:34:37 +08:00
u ' '
2006-11-29 07:14:18 +08:00
>> > p . clean_data
2006-10-29 04:34:37 +08:00
{ ' first_name ' : u ' John ' , ' last_name ' : u ' Lennon ' , ' birthday ' : datetime . date ( 1940 , 10 , 9 ) }
>> > print p [ ' first_name ' ]
< input type = " text " name = " first_name " value = " John " / >
>> > print p [ ' last_name ' ]
< input type = " text " name = " last_name " value = " Lennon " / >
>> > print p [ ' birthday ' ]
2006-10-29 04:59:23 +08:00
< input type = " text " name = " birthday " value = " 1940-10-9 " / >
2006-10-29 04:34:37 +08:00
>> > for boundfield in p :
. . . print boundfield
< input type = " text " name = " first_name " value = " John " / >
< input type = " text " name = " last_name " value = " Lennon " / >
2006-10-29 04:59:23 +08:00
< input type = " text " name = " birthday " value = " 1940-10-9 " / >
2006-11-30 05:48:58 +08:00
>> > for boundfield in p :
. . . print boundfield . verbose_name , boundfield . data
First name John
Last name Lennon
Birthday 1940 - 10 - 9
2006-11-05 04:49:59 +08:00
>> > print p
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " value = " John " / > < / td > < / tr >
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " value = " Lennon " / > < / td > < / tr >
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " value = " 1940-10-9 " / > < / td > < / tr >
2006-10-29 04:34:37 +08:00
2006-11-27 11:49:19 +08:00
Empty dictionaries are valid , too .
>> > p = Person ( { } )
>> > p . errors
{ ' first_name ' : [ u ' This field is required. ' ] , ' last_name ' : [ u ' This field is required. ' ] , ' birthday ' : [ u ' This field is required. ' ] }
>> > p . is_valid ( )
False
>> > print p
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " / > < / td > < / tr >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " / > < / td > < / tr >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " / > < / td > < / tr >
>> > print p . as_table ( )
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " / > < / td > < / tr >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " / > < / td > < / tr >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " / > < / td > < / tr >
>> > print p . as_ul ( )
2006-11-27 12:56:33 +08:00
< li > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > First name : < input type = " text " name = " first_name " / > < / li >
< li > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > Last name : < input type = " text " name = " last_name " / > < / li >
< li > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > Birthday : < input type = " text " name = " birthday " / > < / li >
2006-11-27 11:49:19 +08:00
If you don ' t pass any values to the Form ' s __init__ ( ) , or if you pass None ,
the Form won ' t do any validation. Form.errors will be an empty dictionary *but*
Form . is_valid ( ) will return False .
>> > p = Person ( )
>> > p . errors
{ }
>> > p . is_valid ( )
False
>> > print p
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " / > < / td > < / tr >
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " / > < / td > < / tr >
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " / > < / td > < / tr >
>> > print p . as_table ( )
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " / > < / td > < / tr >
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " / > < / td > < / tr >
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " / > < / td > < / tr >
>> > print p . as_ul ( )
< li > First name : < input type = " text " name = " first_name " / > < / li >
< li > Last name : < input type = " text " name = " last_name " / > < / li >
< li > Birthday : < input type = " text " name = " birthday " / > < / li >
2006-11-27 02:44:58 +08:00
Unicode values are handled properly .
2006-11-27 11:49:19 +08:00
>> > p = Person ( { ' first_name ' : u ' John ' , ' last_name ' : u ' \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 ' , ' birthday ' : ' 1940-10-9 ' } )
2006-11-27 02:44:58 +08:00
>> > p . as_table ( )
2006-11-27 11:49:19 +08:00
u ' <tr><td>First name:</td><td><input type= " text " name= " first_name " value= " John " /></td></tr> \n <tr><td>Last name:</td><td><input type= " text " name= " last_name " value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " /></td></tr> \n <tr><td>Birthday:</td><td><input type= " text " name= " birthday " value= " 1940-10-9 " /></td></tr> '
2006-11-27 02:44:58 +08:00
>> > p . as_ul ( )
2006-11-27 11:49:19 +08:00
u ' <li>First name: <input type= " text " name= " first_name " value= " John " /></li> \n <li>Last name: <input type= " text " name= " last_name " value= " \u0160 \u0110 \u0106 \u017d \u0107 \u017e \u0161 \u0111 " /></li> \n <li>Birthday: <input type= " text " name= " birthday " value= " 1940-10-9 " /></li> '
2006-11-27 02:44:58 +08:00
2006-10-29 04:34:37 +08:00
>> > p = Person ( { ' last_name ' : u ' Lennon ' } )
2006-11-27 09:55:24 +08:00
>> > p . errors
2006-10-29 04:34:37 +08:00
{ ' first_name ' : [ u ' This field is required. ' ] , ' birthday ' : [ u ' This field is required. ' ] }
>> > p . is_valid ( )
False
2006-11-27 09:55:24 +08:00
>> > p . errors . as_ul ( )
2006-10-29 04:34:37 +08:00
u ' <ul class= " errorlist " ><li>first_name<ul class= " errorlist " ><li>This field is required.</li></ul></li><li>birthday<ul class= " errorlist " ><li>This field is required.</li></ul></li></ul> '
2006-11-27 09:55:24 +08:00
>> > print p . errors . as_text ( )
2006-10-29 04:34:37 +08:00
* first_name
* This field is required .
* birthday
* This field is required .
2006-11-29 07:14:18 +08:00
>> > p . clean_data
>> > repr ( p . clean_data )
2006-10-29 04:34:37 +08:00
' None '
>> > p [ ' first_name ' ] . errors
[ u ' This field is required. ' ]
>> > p [ ' first_name ' ] . errors . as_ul ( )
u ' <ul class= " errorlist " ><li>This field is required.</li></ul> '
>> > p [ ' first_name ' ] . errors . as_text ( )
u ' * This field is required. '
>> > p = Person ( )
>> > print p [ ' first_name ' ]
< input type = " text " name = " first_name " / >
>> > print p [ ' last_name ' ]
< input type = " text " name = " last_name " / >
>> > print p [ ' birthday ' ]
< input type = " text " name = " birthday " / >
2006-11-16 07:09:10 +08:00
" auto_id " tells the Form to add an " id " attribute to each form element .
If it ' s a string that contains ' % s ' , Django will use that as a format string
2006-11-29 08:49:27 +08:00
into which the field ' s name will be inserted. It will also put a <label> around
the human - readable labels for a field .
2006-11-16 07:09:10 +08:00
>> > p = Person ( auto_id = ' id_ %s ' )
>> > print p . as_ul ( )
2006-11-29 08:49:27 +08:00
< li > < label for = " id_first_name " > First name : < / label > < input type = " text " name = " first_name " id = " id_first_name " / > < / li >
< li > < label for = " id_last_name " > Last name : < / label > < input type = " text " name = " last_name " id = " id_last_name " / > < / li >
< li > < label for = " id_birthday " > Birthday : < / label > < input type = " text " name = " birthday " id = " id_birthday " / > < / li >
>> > print p . as_table ( )
< tr > < td > < label for = " id_first_name " > First name : < / label > < / td > < td > < input type = " text " name = " first_name " id = " id_first_name " / > < / td > < / tr >
< tr > < td > < label for = " id_last_name " > Last name : < / label > < / td > < td > < input type = " text " name = " last_name " id = " id_last_name " / > < / td > < / tr >
< tr > < td > < label for = " id_birthday " > Birthday : < / label > < / td > < td > < input type = " text " name = " birthday " id = " id_birthday " / > < / td > < / tr >
2006-11-16 07:09:10 +08:00
If auto_id is any True value whose str ( ) does not contain ' %s ' , the " id "
attribute will be the name of the field .
>> > p = Person ( auto_id = True )
>> > print p . as_ul ( )
2006-11-29 08:49:27 +08:00
< li > < label for = " first_name " > First name : < / label > < input type = " text " name = " first_name " id = " first_name " / > < / li >
< li > < label for = " last_name " > Last name : < / label > < input type = " text " name = " last_name " id = " last_name " / > < / li >
< li > < label for = " birthday " > Birthday : < / label > < input type = " text " name = " birthday " id = " birthday " / > < / li >
2006-11-16 07:09:10 +08:00
If auto_id is any False value , an " id " attribute won ' t be output unless it
was manually entered .
>> > p = Person ( auto_id = False )
>> > print p . as_ul ( )
< li > First name : < input type = " text " name = " first_name " / > < / li >
< li > Last name : < input type = " text " name = " last_name " / > < / li >
< li > Birthday : < input type = " text " name = " birthday " / > < / li >
In this example , auto_id is False , but the " id " attribute for the " first_name "
2006-11-29 08:49:27 +08:00
field is given . Also note that field gets a < label > , while the others don ' t.
2006-11-16 07:09:10 +08:00
>> > class PersonNew ( Form ) :
. . . first_name = CharField ( widget = TextInput ( attrs = { ' id ' : ' first_name_id ' } ) )
. . . last_name = CharField ( )
. . . birthday = DateField ( )
>> > p = PersonNew ( auto_id = False )
>> > print p . as_ul ( )
2006-11-29 08:49:27 +08:00
< li > < label for = " first_name_id " > First name : < / label > < input type = " text " id = " first_name_id " name = " first_name " / > < / li >
2006-11-16 07:09:10 +08:00
< li > Last name : < input type = " text " name = " last_name " / > < / li >
< li > Birthday : < input type = " text " name = " birthday " / > < / li >
If the " id " attribute is specified in the Form and auto_id is True , the " id "
attribute in the Form gets precedence .
>> > p = PersonNew ( auto_id = True )
>> > print p . as_ul ( )
2006-11-29 08:49:27 +08:00
< li > < label for = " first_name_id " > First name : < / label > < input type = " text " id = " first_name_id " name = " first_name " / > < / li >
< li > < label for = " last_name " > Last name : < / label > < input type = " text " name = " last_name " id = " last_name " / > < / li >
< li > < label for = " birthday " > Birthday : < / label > < input type = " text " name = " birthday " id = " birthday " / > < / li >
2006-11-16 07:09:10 +08:00
2006-10-29 04:34:37 +08:00
>> > class SignupForm ( Form ) :
. . . email = EmailField ( )
. . . get_spam = BooleanField ( )
>> > f = SignupForm ( )
>> > print f [ ' email ' ]
< input type = " text " name = " email " / >
>> > print f [ ' get_spam ' ]
< input type = " checkbox " name = " get_spam " / >
>> > f = SignupForm ( { ' email ' : ' test@example.com ' , ' get_spam ' : True } )
>> > print f [ ' email ' ]
< input type = " text " name = " email " value = " test@example.com " / >
>> > print f [ ' get_spam ' ]
< input checked = " checked " type = " checkbox " name = " get_spam " / >
Any Field can have a Widget class passed to its constructor :
>> > class ContactForm ( Form ) :
. . . subject = CharField ( )
. . . message = CharField ( widget = Textarea )
>> > f = ContactForm ( )
>> > print f [ ' subject ' ]
< input type = " text " name = " subject " / >
>> > print f [ ' message ' ]
< textarea name = " message " > < / textarea >
2006-11-30 11:58:25 +08:00
as_textarea ( ) , as_text ( ) and as_hidden ( ) are shortcuts for changing the output
widget type :
2006-10-29 04:34:37 +08:00
>> > f [ ' subject ' ] . as_textarea ( )
u ' <textarea name= " subject " ></textarea> '
>> > f [ ' message ' ] . as_text ( )
u ' <input type= " text " name= " message " /> '
2006-11-30 11:58:25 +08:00
>> > f [ ' message ' ] . as_hidden ( )
u ' <input type= " hidden " name= " message " /> '
2006-10-29 04:34:37 +08:00
The ' widget ' parameter to a Field can also be an instance :
>> > class ContactForm ( Form ) :
. . . subject = CharField ( )
. . . message = CharField ( widget = Textarea ( attrs = { ' rows ' : 80 , ' cols ' : 20 } ) )
>> > f = ContactForm ( )
>> > print f [ ' message ' ]
< textarea rows = " 80 " cols = " 20 " name = " message " > < / textarea >
2006-11-30 11:58:25 +08:00
Instance - level attrs are * not * carried over to as_textarea ( ) , as_text ( ) and
as_hidden ( ) :
2006-10-29 04:34:37 +08:00
>> > f [ ' message ' ] . as_text ( )
u ' <input type= " text " name= " message " /> '
>> > f = ContactForm ( { ' subject ' : ' Hello ' , ' message ' : ' I love you. ' } )
>> > f [ ' subject ' ] . as_textarea ( )
u ' <textarea name= " subject " >Hello</textarea> '
>> > f [ ' message ' ] . as_text ( )
u ' <input type= " text " name= " message " value= " I love you. " /> '
2006-11-30 11:58:25 +08:00
>> > f [ ' message ' ] . as_hidden ( )
u ' <input type= " hidden " name= " message " value= " I love you. " /> '
2006-10-29 04:34:37 +08:00
2006-11-02 11:16:12 +08:00
For a form with a < select > , use ChoiceField :
>> > class FrameworkForm ( Form ) :
. . . name = CharField ( )
. . . language = ChoiceField ( choices = [ ( ' P ' , ' Python ' ) , ( ' J ' , ' Java ' ) ] )
>> > f = FrameworkForm ( )
>> > print f [ ' language ' ]
< select name = " language " >
< option value = " P " > Python < / option >
< option value = " J " > Java < / option >
< / select >
>> > f = FrameworkForm ( { ' name ' : ' Django ' , ' language ' : ' P ' } )
>> > print f [ ' language ' ]
< select name = " language " >
< option value = " P " selected = " selected " > Python < / option >
< option value = " J " > Java < / option >
< / select >
2006-11-30 01:00:34 +08:00
Add widget = RadioSelect to use that widget with a ChoiceField .
2006-11-27 02:44:58 +08:00
>> > class FrameworkForm ( Form ) :
. . . name = CharField ( )
. . . language = ChoiceField ( choices = [ ( ' P ' , ' Python ' ) , ( ' J ' , ' Java ' ) ] , widget = RadioSelect )
>> > f = FrameworkForm ( )
>> > print f [ ' language ' ]
< ul >
< li > < label > < input type = " radio " name = " language " value = " P " / > Python < / label > < / li >
< li > < label > < input type = " radio " name = " language " value = " J " / > Java < / label > < / li >
< / ul >
2006-11-29 09:40:27 +08:00
>> > print f
< tr > < td > Name : < / td > < td > < input type = " text " name = " name " / > < / td > < / tr >
< tr > < td > Language : < / td > < td > < ul >
< li > < label > < input type = " radio " name = " language " value = " P " / > Python < / label > < / li >
< li > < label > < input type = " radio " name = " language " value = " J " / > Java < / label > < / li >
< / ul > < / td > < / tr >
>> > print f . as_ul ( )
< li > Name : < input type = " text " name = " name " / > < / li >
< li > Language : < ul >
< li > < label > < input type = " radio " name = " language " value = " P " / > Python < / label > < / li >
< li > < label > < input type = " radio " name = " language " value = " J " / > Java < / label > < / li >
< / ul > < / li >
Regarding auto_id and < label > , RadioSelect is a special case . Each radio button
gets a distinct ID , formed by appending an underscore plus the button ' s
zero - based index .
>> > f = FrameworkForm ( auto_id = ' id_ %s ' )
>> > print f [ ' language ' ]
< ul >
< li > < label > < input type = " radio " id = " id_language_0 " value = " P " name = " language " / > Python < / label > < / li >
< li > < label > < input type = " radio " id = " id_language_1 " value = " J " name = " language " / > Java < / label > < / li >
< / ul >
When RadioSelect is used with auto_id , and the whole form is printed using
either as_table ( ) or as_ul ( ) , the label for the RadioSelect will point to the
ID of the * first * radio button .
>> > print f
< tr > < td > < label for = " id_name " > Name : < / label > < / td > < td > < input type = " text " name = " name " id = " id_name " / > < / td > < / tr >
< tr > < td > < label for = " id_language_0 " > Language : < / label > < / td > < td > < ul >
< li > < label > < input type = " radio " id = " id_language_0 " value = " P " name = " language " / > Python < / label > < / li >
< li > < label > < input type = " radio " id = " id_language_1 " value = " J " name = " language " / > Java < / label > < / li >
< / ul > < / td > < / tr >
>> > print f . as_ul ( )
< li > < label for = " id_name " > Name : < / label > < input type = " text " name = " name " id = " id_name " / > < / li >
< li > < label for = " id_language_0 " > Language : < / label > < ul >
< li > < label > < input type = " radio " id = " id_language_0 " value = " P " name = " language " / > Python < / label > < / li >
< li > < label > < input type = " radio " id = " id_language_1 " value = " J " name = " language " / > Java < / label > < / li >
< / ul > < / li >
2006-11-27 02:44:58 +08:00
2006-11-02 11:16:12 +08:00
MultipleChoiceField is a special case , as its data is required to be a list :
>> > class SongForm ( Form ) :
. . . name = CharField ( )
. . . composers = MultipleChoiceField ( )
>> > f = SongForm ( )
>> > print f [ ' composers ' ]
< select multiple = " multiple " name = " composers " >
< / select >
>> > class SongForm ( Form ) :
. . . name = CharField ( )
. . . composers = MultipleChoiceField ( choices = [ ( ' J ' , ' John Lennon ' ) , ( ' P ' , ' Paul McCartney ' ) ] )
>> > f = SongForm ( )
>> > print f [ ' composers ' ]
< select multiple = " multiple " name = " composers " >
< option value = " J " > John Lennon < / option >
< option value = " P " > Paul McCartney < / option >
< / select >
>> > f = SongForm ( { ' name ' : ' Yesterday ' , ' composers ' : [ ' P ' ] } )
>> > print f [ ' name ' ]
< input type = " text " name = " name " value = " Yesterday " / >
>> > print f [ ' composers ' ]
< select multiple = " multiple " name = " composers " >
< option value = " J " > John Lennon < / option >
< option value = " P " selected = " selected " > Paul McCartney < / option >
< / select >
2006-11-05 04:49:59 +08:00
2006-11-30 01:00:34 +08:00
MultipleChoiceField can also be used with the CheckboxSelectMultiple widget .
>> > class SongForm ( Form ) :
. . . name = CharField ( )
. . . composers = MultipleChoiceField ( choices = [ ( ' J ' , ' John Lennon ' ) , ( ' P ' , ' Paul McCartney ' ) ] , widget = CheckboxSelectMultiple )
>> > f = SongForm ( )
>> > print f [ ' composers ' ]
< ul >
< li > < label > < input type = " checkbox " name = " composersJ " / > John Lennon < / label > < / li >
< li > < label > < input type = " checkbox " name = " composersP " / > Paul McCartney < / label > < / li >
< / ul >
>> > f = SongForm ( { ' composers ' : [ ' J ' ] } )
>> > print f [ ' composers ' ]
< ul >
< li > < label > < input checked = " checked " type = " checkbox " name = " composersJ " / > John Lennon < / label > < / li >
< li > < label > < input type = " checkbox " name = " composersP " / > Paul McCartney < / label > < / li >
< / ul >
>> > f = SongForm ( { ' composers ' : [ ' J ' , ' P ' ] } )
>> > print f [ ' composers ' ]
< ul >
< li > < label > < input checked = " checked " type = " checkbox " name = " composersJ " / > John Lennon < / label > < / li >
< li > < label > < input checked = " checked " type = " checkbox " name = " composersP " / > Paul McCartney < / label > < / li >
< / ul >
When using CheckboxSelectMultiple , the framework automatically converts the
data in clean_data to a list of values , rather than the underlying HTML form
field name .
>> > f = SongForm ( { ' name ' : ' Yesterday ' } )
>> > f . errors
{ ' composers ' : [ u ' This field is required. ' ] }
>> > f = SongForm ( { ' name ' : ' Yesterday ' , ' composersJ ' : ' on ' } )
>> > f . errors
{ }
>> > f . clean_data
{ ' composers ' : [ u ' J ' ] , ' name ' : u ' Yesterday ' }
>> > f = SongForm ( { ' name ' : ' Yesterday ' , ' composersJ ' : ' on ' , ' composersP ' : ' on ' } )
>> > f . errors
{ }
>> > f . clean_data
{ ' composers ' : [ u ' J ' , u ' P ' ] , ' name ' : u ' Yesterday ' }
2006-11-05 04:49:59 +08:00
There are a couple of ways to do multiple - field validation . If you want the
validation message to be associated with a particular field , implement the
clean_XXX ( ) method on the Form , where XXX is the field name . As in
2006-11-27 11:49:19 +08:00
Field . clean ( ) , the clean_XXX ( ) method should return the cleaned value . In the
clean_XXX ( ) method , you have access to self . clean_data , which is a dictionary
of all the data that has been cleaned * so far * , in order by the fields ,
including the current field ( e . g . , the field XXX if you ' re in clean_XXX()).
2006-11-05 04:49:59 +08:00
>> > class UserRegistration ( Form ) :
. . . username = CharField ( max_length = 10 )
. . . password1 = CharField ( widget = PasswordInput )
. . . password2 = CharField ( widget = PasswordInput )
. . . def clean_password2 ( self ) :
. . . if self . clean_data . get ( ' password1 ' ) and self . clean_data . get ( ' password2 ' ) and self . clean_data [ ' password1 ' ] != self . clean_data [ ' password2 ' ] :
. . . raise ValidationError ( u ' Please make sure your passwords match. ' )
. . . return self . clean_data [ ' password2 ' ]
>> > f = UserRegistration ( )
2006-11-27 09:55:24 +08:00
>> > f . errors
2006-11-27 11:49:19 +08:00
{ }
>> > f = UserRegistration ( { } )
>> > f . errors
2006-11-05 04:49:59 +08:00
{ ' username ' : [ u ' This field is required. ' ] , ' password1 ' : [ u ' This field is required. ' ] , ' password2 ' : [ u ' This field is required. ' ] }
>> > f = UserRegistration ( { ' username ' : ' adrian ' , ' password1 ' : ' foo ' , ' password2 ' : ' bar ' } )
2006-11-27 09:55:24 +08:00
>> > f . errors
2006-11-05 04:49:59 +08:00
{ ' password2 ' : [ u ' Please make sure your passwords match. ' ] }
>> > f = UserRegistration ( { ' username ' : ' adrian ' , ' password1 ' : ' foo ' , ' password2 ' : ' foo ' } )
2006-11-27 09:55:24 +08:00
>> > f . errors
2006-11-05 04:49:59 +08:00
{ }
2006-11-29 07:14:18 +08:00
>> > f . clean_data
2006-11-05 04:49:59 +08:00
{ ' username ' : u ' adrian ' , ' password1 ' : u ' foo ' , ' password2 ' : u ' foo ' }
Another way of doing multiple - field validation is by implementing the
Form ' s clean() method. If you do this, any ValidationError raised by that
method will not be associated with a particular field ; it will have a
2006-11-27 11:49:19 +08:00
special - case association with the field named ' __all__ ' .
Note that in Form . clean ( ) , you have access to self . clean_data , a dictionary of
all the fields / values that have * not * raised a ValidationError . Also note
Form . clean ( ) is required to return a dictionary of all clean data .
2006-11-05 04:49:59 +08:00
>> > class UserRegistration ( Form ) :
. . . username = CharField ( max_length = 10 )
. . . password1 = CharField ( widget = PasswordInput )
. . . password2 = CharField ( widget = PasswordInput )
. . . def clean ( self ) :
. . . if self . clean_data . get ( ' password1 ' ) and self . clean_data . get ( ' password2 ' ) and self . clean_data [ ' password1 ' ] != self . clean_data [ ' password2 ' ] :
. . . raise ValidationError ( u ' Please make sure your passwords match. ' )
. . . return self . clean_data
>> > f = UserRegistration ( )
2006-11-27 11:49:19 +08:00
>> > f . errors
{ }
>> > f = UserRegistration ( { } )
2006-11-05 04:49:59 +08:00
>> > print f . as_table ( )
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-05 04:49:59 +08:00
< tr > < td > Username : < / td > < td > < input type = " text " name = " username " / > < / td > < / tr >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-05 04:49:59 +08:00
< tr > < td > Password1 : < / td > < td > < input type = " password " name = " password1 " / > < / td > < / tr >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > This field is required . < / li > < / ul > < / td > < / tr >
2006-11-05 04:49:59 +08:00
< tr > < td > Password2 : < / td > < td > < input type = " password " name = " password2 " / > < / td > < / tr >
2006-11-27 09:55:24 +08:00
>> > f . errors
2006-11-05 04:49:59 +08:00
{ ' username ' : [ u ' This field is required. ' ] , ' password1 ' : [ u ' This field is required. ' ] , ' password2 ' : [ u ' This field is required. ' ] }
>> > f = UserRegistration ( { ' username ' : ' adrian ' , ' password1 ' : ' foo ' , ' password2 ' : ' bar ' } )
2006-11-27 09:55:24 +08:00
>> > f . errors
2006-11-05 04:49:59 +08:00
{ ' __all__ ' : [ u ' Please make sure your passwords match. ' ] }
>> > print f . as_table ( )
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > Please make sure your passwords match . < / li > < / ul > < / td > < / tr >
2006-11-05 04:49:59 +08:00
< tr > < td > Username : < / td > < td > < input type = " text " name = " username " value = " adrian " / > < / td > < / tr >
< tr > < td > Password1 : < / td > < td > < input type = " password " name = " password1 " value = " foo " / > < / td > < / tr >
< tr > < td > Password2 : < / td > < td > < input type = " password " name = " password2 " value = " bar " / > < / td > < / tr >
2006-11-27 11:49:19 +08:00
>> > print f . as_ul ( )
2006-11-27 12:56:33 +08:00
< li > < ul class = " errorlist " > < li > Please make sure your passwords match . < / li > < / ul > < / li >
2006-11-05 04:49:59 +08:00
< li > Username : < input type = " text " name = " username " value = " adrian " / > < / li >
< li > Password1 : < input type = " password " name = " password1 " value = " foo " / > < / li >
< li > Password2 : < input type = " password " name = " password2 " value = " bar " / > < / li >
>> > f = UserRegistration ( { ' username ' : ' adrian ' , ' password1 ' : ' foo ' , ' password2 ' : ' foo ' } )
2006-11-27 09:55:24 +08:00
>> > f . errors
2006-11-05 04:49:59 +08:00
{ }
2006-11-29 07:14:18 +08:00
>> > f . clean_data
2006-11-05 04:49:59 +08:00
{ ' username ' : u ' adrian ' , ' password1 ' : u ' foo ' , ' password2 ' : u ' foo ' }
2006-11-16 07:17:00 +08:00
It ' s possible to construct a Form dynamically by adding to the self.fields
dictionary in __init__ ( ) . Don ' t forget to call Form.__init__() within the
subclass ' __init__().
>> > class Person ( Form ) :
. . . first_name = CharField ( )
. . . last_name = CharField ( )
. . . def __init__ ( self ) :
. . . super ( Person , self ) . __init__ ( )
. . . self . fields [ ' birthday ' ] = DateField ( )
>> > p = Person ( )
>> > print p
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " / > < / td > < / tr >
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " / > < / td > < / tr >
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " / > < / td > < / tr >
2006-11-24 01:16:15 +08:00
2006-12-01 01:07:40 +08:00
HiddenInput widgets are displayed differently in the as_table ( ) and as_ul ( )
output of a Form - - their verbose names are not displayed , and a separate
< tr > / < li > is not displayed .
>> > class Person ( Form ) :
. . . first_name = CharField ( )
. . . last_name = CharField ( )
. . . hidden_text = CharField ( widget = HiddenInput )
. . . birthday = DateField ( )
>> > p = Person ( )
>> > print p
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " / > < / td > < / tr >
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " / > < / td > < / tr >
< input type = " hidden " name = " hidden_text " / >
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " / > < / td > < / tr >
>> > print p . as_ul ( )
< li > First name : < input type = " text " name = " first_name " / > < / li >
< li > Last name : < input type = " text " name = " last_name " / > < / li >
< input type = " hidden " name = " hidden_text " / >
< li > Birthday : < input type = " text " name = " birthday " / > < / li >
With auto_id set , a HiddenInput still gets an ID , but it doesn ' t get a label.
>> > p = Person ( auto_id = ' id_ %s ' )
>> > print p
< tr > < td > < label for = " id_first_name " > First name : < / label > < / td > < td > < input type = " text " name = " first_name " id = " id_first_name " / > < / td > < / tr >
< tr > < td > < label for = " id_last_name " > Last name : < / label > < / td > < td > < input type = " text " name = " last_name " id = " id_last_name " / > < / td > < / tr >
< input type = " hidden " name = " hidden_text " id = " id_hidden_text " / >
< tr > < td > < label for = " id_birthday " > Birthday : < / label > < / td > < td > < input type = " text " name = " birthday " id = " id_birthday " / > < / td > < / tr >
>> > print p . as_ul ( )
< li > < label for = " id_first_name " > First name : < / label > < input type = " text " name = " first_name " id = " id_first_name " / > < / li >
< li > < label for = " id_last_name " > Last name : < / label > < input type = " text " name = " last_name " id = " id_last_name " / > < / li >
< input type = " hidden " name = " hidden_text " id = " id_hidden_text " / >
< li > < label for = " id_birthday " > Birthday : < / label > < input type = " text " name = " birthday " id = " id_birthday " / > < / li >
If a field with a HiddenInput has errors , the as_table ( ) and as_ul ( ) output
will include the error message ( s ) with the text " (Hidden field [fieldname]) "
prepended .
>> > p = Person ( { ' first_name ' : ' John ' , ' last_name ' : ' Lennon ' , ' birthday ' : ' 1940-10-9 ' } )
>> > print p
< tr > < td > First name : < / td > < td > < input type = " text " name = " first_name " value = " John " / > < / td > < / tr >
< tr > < td > Last name : < / td > < td > < input type = " text " name = " last_name " value = " Lennon " / > < / td > < / tr >
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > ( Hidden field hidden_text ) This field is required . < / li > < / ul > < / td > < / tr >
< input type = " hidden " name = " hidden_text " / >
< tr > < td > Birthday : < / td > < td > < input type = " text " name = " birthday " value = " 1940-10-9 " / > < / td > < / tr >
>> > print p . as_ul ( )
< li > First name : < input type = " text " name = " first_name " value = " John " / > < / li >
< li > Last name : < input type = " text " name = " last_name " value = " Lennon " / > < / li >
< li > < ul class = " errorlist " > < li > ( Hidden field hidden_text ) This field is required . < / li > < / ul > < / li >
< input type = " hidden " name = " hidden_text " / >
< li > Birthday : < input type = " text " name = " birthday " value = " 1940-10-9 " / > < / li >
2006-11-24 01:16:15 +08:00
A Form ' s fields are displayed in the same order in which they were defined.
>> > class TestForm ( Form ) :
. . . field1 = CharField ( )
. . . field2 = CharField ( )
. . . field3 = CharField ( )
. . . field4 = CharField ( )
. . . field5 = CharField ( )
. . . field6 = CharField ( )
. . . field7 = CharField ( )
. . . field8 = CharField ( )
. . . field9 = CharField ( )
. . . field10 = CharField ( )
. . . field11 = CharField ( )
. . . field12 = CharField ( )
. . . field13 = CharField ( )
. . . field14 = CharField ( )
>> > p = TestForm ( )
>> > print p
< tr > < td > Field1 : < / td > < td > < input type = " text " name = " field1 " / > < / td > < / tr >
< tr > < td > Field2 : < / td > < td > < input type = " text " name = " field2 " / > < / td > < / tr >
< tr > < td > Field3 : < / td > < td > < input type = " text " name = " field3 " / > < / td > < / tr >
< tr > < td > Field4 : < / td > < td > < input type = " text " name = " field4 " / > < / td > < / tr >
< tr > < td > Field5 : < / td > < td > < input type = " text " name = " field5 " / > < / td > < / tr >
< tr > < td > Field6 : < / td > < td > < input type = " text " name = " field6 " / > < / td > < / tr >
< tr > < td > Field7 : < / td > < td > < input type = " text " name = " field7 " / > < / td > < / tr >
< tr > < td > Field8 : < / td > < td > < input type = " text " name = " field8 " / > < / td > < / tr >
< tr > < td > Field9 : < / td > < td > < input type = " text " name = " field9 " / > < / td > < / tr >
< tr > < td > Field10 : < / td > < td > < input type = " text " name = " field10 " / > < / td > < / tr >
< tr > < td > Field11 : < / td > < td > < input type = " text " name = " field11 " / > < / td > < / tr >
< tr > < td > Field12 : < / td > < td > < input type = " text " name = " field12 " / > < / td > < / tr >
< tr > < td > Field13 : < / td > < td > < input type = " text " name = " field13 " / > < / td > < / tr >
< tr > < td > Field14 : < / td > < td > < input type = " text " name = " field14 " / > < / td > < / tr >
2006-11-27 11:49:19 +08:00
2006-11-27 12:49:26 +08:00
# Basic form processing in a view #############################################
2006-11-27 11:49:19 +08:00
>> > from django . template import Template , Context
>> > class UserRegistration ( Form ) :
. . . username = CharField ( max_length = 10 )
. . . password1 = CharField ( widget = PasswordInput )
. . . password2 = CharField ( widget = PasswordInput )
. . . def clean ( self ) :
. . . if self . clean_data . get ( ' password1 ' ) and self . clean_data . get ( ' password2 ' ) and self . clean_data [ ' password1 ' ] != self . clean_data [ ' password2 ' ] :
. . . raise ValidationError ( u ' Please make sure your passwords match. ' )
. . . return self . clean_data
>> > def my_function ( method , post_data ) :
. . . if method == ' POST ' :
. . . form = UserRegistration ( post_data )
. . . else :
. . . form = UserRegistration ( )
. . . if form . is_valid ( ) :
2006-11-29 07:14:18 +08:00
. . . return ' VALID: %r ' % form . clean_data
2006-11-27 11:49:19 +08:00
. . . t = Template ( ' <form action= " " method= " post " > \n <table> \n {{ form }} \n </table> \n <input type= " submit " /> \n </form> ' )
. . . return t . render ( Context ( { ' form ' : form } ) )
Case 1 : GET ( an empty form , with no errors ) .
>> > print my_function ( ' GET ' , { } )
< form action = " " method = " post " >
< table >
< tr > < td > Username : < / td > < td > < input type = " text " name = " username " / > < / td > < / tr >
< tr > < td > Password1 : < / td > < td > < input type = " password " name = " password1 " / > < / td > < / tr >
< tr > < td > Password2 : < / td > < td > < input type = " password " name = " password2 " / > < / td > < / tr >
< / table >
< input type = " submit " / >
< / form >
Case 2 : POST with erroneous data ( a redisplayed form , with errors ) .
>> > print my_function ( ' POST ' , { ' username ' : ' this-is-a-long-username ' , ' password1 ' : ' foo ' , ' password2 ' : ' bar ' } )
< form action = " " method = " post " >
< table >
2006-11-27 12:56:33 +08:00
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > Please make sure your passwords match . < / li > < / ul > < / td > < / tr >
< tr > < td colspan = " 2 " > < ul class = " errorlist " > < li > Ensure this value has at most 10 characters . < / li > < / ul > < / td > < / tr >
2006-11-27 11:49:19 +08:00
< tr > < td > Username : < / td > < td > < input type = " text " name = " username " value = " this-is-a-long-username " / > < / td > < / tr >
< tr > < td > Password1 : < / td > < td > < input type = " password " name = " password1 " value = " foo " / > < / td > < / tr >
< tr > < td > Password2 : < / td > < td > < input type = " password " name = " password2 " value = " bar " / > < / td > < / tr >
< / table >
< input type = " submit " / >
< / form >
Case 3 : POST with valid data ( the success message ) .
>> > print my_function ( ' POST ' , { ' username ' : ' adrian ' , ' password1 ' : ' secret ' , ' password2 ' : ' secret ' } )
2006-11-29 07:14:18 +08:00
VALID : { ' username ' : u ' adrian ' , ' password1 ' : u ' secret ' , ' password2 ' : u ' secret ' }
2006-11-27 12:49:26 +08:00
# Some ideas for using templates with forms ###################################
>> > class UserRegistration ( Form ) :
. . . username = CharField ( max_length = 10 )
. . . password1 = CharField ( widget = PasswordInput )
. . . password2 = CharField ( widget = PasswordInput )
. . . def clean ( self ) :
. . . if self . clean_data . get ( ' password1 ' ) and self . clean_data . get ( ' password2 ' ) and self . clean_data [ ' password1 ' ] != self . clean_data [ ' password2 ' ] :
. . . raise ValidationError ( u ' Please make sure your passwords match. ' )
. . . return self . clean_data
You have full flexibility in displaying form fields in a template . Just pass a
Form instance to the template , and use " dot " access to refer to individual
fields . Note , however , that this flexibility comes with the responsibility of
displaying all the errors , including any that might not be associated with a
particular field .
>> > t = Template ( ''' <form action= " " >
. . . { { form . username . errors . as_ul } } < p > < label > Your username : { { form . username } } < / label > < / p >
. . . { { form . password1 . errors . as_ul } } < p > < label > Password : { { form . password1 } } < / label > < / p >
. . . { { form . password2 . errors . as_ul } } < p > < label > Password ( again ) : { { form . password2 } } < / label > < / p >
. . . < input type = " submit " / >
. . . < / form > ''' )
>> > print t . render ( Context ( { ' form ' : UserRegistration ( ) } ) )
< form action = " " >
< p > < label > Your username : < input type = " text " name = " username " / > < / label > < / p >
< p > < label > Password : < input type = " password " name = " password1 " / > < / label > < / p >
< p > < label > Password ( again ) : < input type = " password " name = " password2 " / > < / label > < / p >
< input type = " submit " / >
< / form >
>> > print t . render ( Context ( { ' form ' : UserRegistration ( { ' username ' : ' django ' } ) } ) )
< form action = " " >
< p > < label > Your username : < input type = " text " name = " username " value = " django " / > < / label > < / p >
< ul class = " errorlist " > < li > This field is required . < / li > < / ul > < p > < label > Password : < input type = " password " name = " password1 " / > < / label > < / p >
< ul class = " errorlist " > < li > This field is required . < / li > < / ul > < p > < label > Password ( again ) : < input type = " password " name = " password2 " / > < / label > < / p >
< input type = " submit " / >
< / form >
2006-11-29 08:49:27 +08:00
Use form . [ field ] . verbose_name to output a field ' s " verbose name " -- its field
name with underscores converted to spaces , and the initial letter capitalized .
>> > t = Template ( ''' <form action= " " >
. . . < p > < label > { { form . username . verbose_name } } : { { form . username } } < / label > < / p >
. . . < p > < label > { { form . password1 . verbose_name } } : { { form . password1 } } < / label > < / p >
. . . < p > < label > { { form . password2 . verbose_name } } : { { form . password2 } } < / label > < / p >
. . . < input type = " submit " / >
. . . < / form > ''' )
>> > print t . render ( Context ( { ' form ' : UserRegistration ( ) } ) )
< form action = " " >
< p > < label > Username : < input type = " text " name = " username " / > < / label > < / p >
< p > < label > Password1 : < input type = " password " name = " password1 " / > < / label > < / p >
< p > < label > Password2 : < input type = " password " name = " password2 " / > < / label > < / p >
< input type = " submit " / >
< / form >
User form . [ field ] . label_tag to output a field ' s verbose_name with a <label>
tag wrapped around it , but * only * if the given field has an " id " attribute .
Recall from above that passing the " auto_id " argument to a Form gives each
field an " id " attribute .
>> > t = Template ( ''' <form action= " " >
. . . < p > { { form . username . label_tag } } : { { form . username } } < / p >
. . . < p > { { form . password1 . label_tag } } : { { form . password1 } } < / p >
. . . < p > { { form . password2 . label_tag } } : { { form . password2 } } < / p >
. . . < input type = " submit " / >
. . . < / form > ''' )
>> > print t . render ( Context ( { ' form ' : UserRegistration ( ) } ) )
< form action = " " >
< p > Username : < input type = " text " name = " username " / > < / p >
< p > Password1 : < input type = " password " name = " password1 " / > < / p >
< p > Password2 : < input type = " password " name = " password2 " / > < / p >
< input type = " submit " / >
< / form >
>> > print t . render ( Context ( { ' form ' : UserRegistration ( auto_id = ' id_ %s ' ) } ) )
< form action = " " >
< p > < label for = " id_username " > Username < / label > : < input type = " text " name = " username " id = " id_username " / > < / p >
< p > < label for = " id_password1 " > Password1 < / label > : < input type = " password " name = " password1 " id = " id_password1 " / > < / p >
< p > < label for = " id_password2 " > Password2 < / label > : < input type = " password " name = " password2 " id = " id_password2 " / > < / p >
< input type = " submit " / >
< / form >
2006-11-27 12:49:26 +08:00
To display the errors that aren ' t associated with a particular field -- e.g.,
the errors caused by Form . clean ( ) - - use { { form . non_field_errors } } in the
template . If used on its own , it is displayed as a < ul > ( or an empty string , if
the list of errors is empty ) . You can also use it in { % if % } statements .
2006-11-29 08:49:27 +08:00
>> > t = Template ( ''' <form action= " " >
. . . { { form . username . errors . as_ul } } < p > < label > Your username : { { form . username } } < / label > < / p >
. . . { { form . password1 . errors . as_ul } } < p > < label > Password : { { form . password1 } } < / label > < / p >
. . . { { form . password2 . errors . as_ul } } < p > < label > Password ( again ) : { { form . password2 } } < / label > < / p >
. . . < input type = " submit " / >
. . . < / form > ''' )
2006-11-27 12:49:26 +08:00
>> > print t . render ( Context ( { ' form ' : UserRegistration ( { ' username ' : ' django ' , ' password1 ' : ' foo ' , ' password2 ' : ' bar ' } ) } ) )
< form action = " " >
< p > < label > Your username : < input type = " text " name = " username " value = " django " / > < / label > < / p >
< p > < label > Password : < input type = " password " name = " password1 " value = " foo " / > < / label > < / p >
< p > < label > Password ( again ) : < input type = " password " name = " password2 " value = " bar " / > < / label > < / p >
< input type = " submit " / >
< / form >
>> > t = Template ( ''' <form action= " " >
. . . { { form . non_field_errors } }
. . . { { form . username . errors . as_ul } } < p > < label > Your username : { { form . username } } < / label > < / p >
. . . { { form . password1 . errors . as_ul } } < p > < label > Password : { { form . password1 } } < / label > < / p >
. . . { { form . password2 . errors . as_ul } } < p > < label > Password ( again ) : { { form . password2 } } < / label > < / p >
. . . < input type = " submit " / >
. . . < / form > ''' )
>> > print t . render ( Context ( { ' form ' : UserRegistration ( { ' username ' : ' django ' , ' password1 ' : ' foo ' , ' password2 ' : ' bar ' } ) } ) )
< form action = " " >
< ul class = " errorlist " > < li > Please make sure your passwords match . < / li > < / ul >
< p > < label > Your username : < input type = " text " name = " username " value = " django " / > < / label > < / p >
< p > < label > Password : < input type = " password " name = " password1 " value = " foo " / > < / label > < / p >
< p > < label > Password ( again ) : < input type = " password " name = " password2 " value = " bar " / > < / label > < / p >
< input type = " submit " / >
< / form >
2006-10-29 04:34:37 +08:00
"""
if __name__ == " __main__ " :
import doctest
doctest . testmod ( )