Form.prefix
You can put several Django forms inside one <form>
tag. To give each Form
its own namespace, use the prefix
keyword argument:
1 2 3 4 5 6 7 8 | >>> mother = PersonForm(prefix = "mother" ) >>> father = PersonForm(prefix = "father" ) >>> print (mother.as_ul()) <li><label for = "id_mother-first_name" >First name:< / label> < input type = "text" name = "mother-first_name" id = "id_mother-first_name" required / >< / li> <li><label for = "id_mother-last_name" >Last name:< / label> < input type = "text" name = "mother-last_name" id = "id_mother-last_name" required / >< / li> >>> print (father.as_ul()) <li><label for = "id_father-first_name" >First name:< / label> < input type = "text" name = "father-first_name" id = "id_father-first_name" required / >< / li> <li><label for = "id_father-last_name" >Last name:< / label> < input type = "text" name = "father-last_name" id = "id_father-last_name" required / >< / li> |
The prefix can also be specified on the form class:
1 2 3 | >>> class PersonForm(forms.Form): ... ... ... prefix = 'person' |
New in Django 1.9:
The ability to specify prefix
on the form class was added.
Please login to continue.