#StackBounty: #python #django #python-3.x #forms #django-formwizard Conditional Form Field in Django wizard form

Bounty: 50

I am using wizard forms in django. I want to create a form field only if answer to some other form field is marked “yes” otherwise I don’t want this new form field. How can I do this ?
I have tried some other answers related to this but most of them tells about how to mark field required or not but I want to display that field only if answer to other field is “Yes”

Django Form Wizard with Conditional Questions

In below code I want to display field “Pool2” only if answer to field “Pool” is marked “yes” otherwise I don’t want that field. Basically I want to get some details of pool in field “Pool2” if there is pool in user’s house.

forms.py

class ListingForm2(forms.Form):
     Pool = (
        ("Yes","Yes"),
        ("No","No"),
    )
    Pool = forms.ChoiceField(choices = Pool,label = "Does your property have a pool ?")
    Pool2 = forms.CharField(required=False)

Views.py

class ListingWizard(SessionWizardView):
    template_name = 'listing_form.html'
    form_list = [ListingForm1,ListingForm2,ListingForm3,ListingForm4]
    def done(self, form_list, **kwargs):
        save_data(form.cleaned_data for form in form_list)
        return render(self.request,'done.html',{
            'form_data' : [form.cleaned_data for form in form_list],
            })


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.