python - Django Form Wizard with Conditional Questions -


in django application, have form wizard few form classes. have ability have conditional questions. meaning if user selects yes question, question within form become required , javascript make question visible. found example of how online, doesn't work. suggestions on how can create functionality?

class questionform(forms.form):  cool_list = (     ('cool','cool'),     ('really cool','really cool'), )  yes, no = 'yes','no'  yes_no = (     (yes,'yes'),     (no,'no'), )  are_you_cool = forms.choicefield(choices=yes_no,label='are cool?') how_cool = forms.multiplechoicefield(required=false,widget=checkboxselectmultiple, choices=cool_list,label='how cool you?')  def __init__(self, data=none, *args, **kwargs):     super(questionform, self).__init__(data, *args, **kwargs)      if data , data.get('are_you_cool', none) == self.yes:         self.fields['how_cool'].required = true 

try replace __init__ method of form custom clean_are_you_cool method. so, if user submit value yes should check if how_cool field populated too. should on client side provide great user experience. forms:

def clean_are_you_cool(self):     if self.cleaned_data.get('are_you_cool', none) == 'yes':         if self.cleaned_data.get('how_cool', none) not none:            #  actions cool user.             pass     #  or if user not cool. 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -