Conditional Form Field in Django wizard form
NickName:Kanish Anand Ask DateTime:2019-07-13T22:38:51

Conditional Form Field in Django wizard form

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],
            })

Copyright Notice:Content Author:「Kanish Anand」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57020127/conditional-form-field-in-django-wizard-form

More about “Conditional Form Field in Django wizard form” related questions

Conditional Form Field in Django wizard form

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...

Show Detail

Django Form Wizard with Conditional Questions

In my Django application, I currently have a form wizard with a few form classes. I would like to have the ability to have conditional questions. Meaning if the user selects yes for a certain que...

Show Detail

django wizard form problem in showing multiforms

My Views Page: from django.shortcuts import render from django.http import HttpResponse from formtools.wizard.views import SessionWizardView from .forms import ContactForm1,ContactForm2,ContactFor...

Show Detail

Repeating forms in django form wizard

I've created a form wizard in django which starts with a form which the user can enter their details (name, age etc.) into. The wizard then goes on to some other forms I need. Now I want to expan...

Show Detail

Django Form Wizard With Fieldsets

I'm using the Django Form Wizard (From 1.8 located in formtools) to split a form into multiple steps. This works fine and all. But I'm interested in making fieldsets in my form and splitting my form

Show Detail

Django form wizard - customized form layout for each step

At the moment I'm experimenting with django form wizard. The basic set-up works now and I'm able to call different templates for each step showing variable text. Now I want to take it a step furth...

Show Detail

Django form wizard cannot prevent form submission with Javascript

I am using a form wizard in django and I want to allow the form to submit only if a Javascript condition is true. For starters, let's say that I always want to block the form submission. So, I am ...

Show Detail

Binding Files to Form Django Wizard

So I was unsuccessful at hooking up the Session-based wizard from django-merlin, but I am trying again with the wizard that is included in the django source. However, when trying to upload files us...

Show Detail

Styling django widget in wizard form

I am facing an issue with widgets in forms. I have 3 steps ( 3 forms ) in a django wizard and I want to style each field. If I loop the forms to style each field separately it seems to work but, it

Show Detail

Setting up Django Form Wizard

I'm attempting to set up a Django Form Wizard, but I'm having trouble getting it to work. I've followed the example, from the Django website, but can't get anything to work. When I go to the URL that

Show Detail