python 3.x - Django Formset Custom Field Exclusion -
i created form has 2 radio buttons (male/female). below have input box first name , last name. if use formset replicate form, include radio buttons every input box , wanted exclude that. assume once user select "male" names should matches gender selection.
the output should so:
o male o female
first name: _______________ last name: _______________
first name: _______________ last name: _______________
...
forms.py django import forms class nameform (forms.form): gender = ( ('male', 'male'), ('female', 'female') ) gender = forms.choicefield (widget = forms.radioselect, choices = gender) first_name = forms.charfield (max_length = 20) last_name = forms.charfield (max_length = 20) shell output: >>> django.forms.formsets import formset_factory >>> nameform.forms import nameform >>> >>> nameformset = formset_factory (nameform, = 2) >>> formset = nameformset () >>> form in formset: ... print (form.as_table()) ... <tr><th><label for="id_form-0-gender_0">gender:</label></th><td><ul id="id_form-0-gender"> <li><label for="id_form-0-gender_0"><input id="id_form-0-gender_0" name="form-0-gender" type="radio" value="male" /> male</label></li> <li><label for="id_form-0-gender_1"><input id="id_form-0-gender_1" name="form-0-gender" type="radio" value="female" /> female</label></li> </ul></td></tr> <tr><th><label for="id_form-0-first_name">first name:</label></th><td><input id="id_form-0-first_name" maxlength="20" name="form-0-first_name" type="text" /></td></tr> <tr><th><label for="id_form-0-last_name">last name:</label></th><td><input id="id_form-0-last_name" maxlength="20" name="form-0-last_name" type="text" /></td></tr> <tr><th><label for="id_form-1-gender_0">gender:</label></th><td><ul id="id_form-1-gender"> <li><label for="id_form-1-gender_0"><input id="id_form-1-gender_0" name="form-1-gender" type="radio" value="male" /> male</label></li> <li><label for="id_form-1-gender_1"><input id="id_form-1-gender_1" name="form-1-gender" type="radio" value="female" /> female</label></li> </ul></td></tr> <tr><th><label for="id_form-1-first_name">first name:</label></th><td><input id="id_form-1-first_name" maxlength="20" name="form-1-first_name" type="text" /></td></tr> <tr><th><label for="id_form-1-last_name">last name:</label></th><td><input id="id_form-1-last_name" maxlength="20" name="form-1-last_name" type="text" /></td></tr>
you need form , formset. form containing names, formset containing repeating elements.
it doesn't make sense use repeating form class, hack treat first 1 differently.
alternatively if data problem (say legacy interface), , form needs contain both.. potentially hide other elements , use javascript fill in hidden gender selections.
really though, should using form
, formset
.
{{ form.as_p }} {% form in formset %} {{ form.as_p }} {% endfor %}
Comments
Post a Comment