I have a multi-step form that is 3 steps but it has 4 forms. In the first form, the user has to choose from two choices. The user’s first form choices will determine the next form that will be displayed.
The code is not working as it should. The first form choices are radio buttons which are commercial and private. If the user chooses commercial it brings the next form and if the user chose private it brings a different form. My problem is that if the user chose private or commercial it brings the same form.
Views.py
from django.shortcuts import render
from datacollector.forms import CustomerTypeForm, CustomerInfoForm1, CustomerInfoForm2, CustomerDocumentForm
from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect
# Create your views here.
def step1formview(request):
initial = {'customer_chioces': request.session.get('customer_chioces', None), }
form = CustomerTypeForm(request.POST or None, initial=initial)
if request.method == 'POST':
if form.is_valid():
if form.customer_choices.choices == Commercial:
return HttpResponseRedirect(reverse('datacollector:step2aformview'))
else:
return HttpResponseRedirect(reverse('datacollector:step2bformview'))
return render(request, 'forms/CustomerTypeForm.html', {'form': form})
template
{% extends 'base.html' %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
{% block head %}
{% endblock %}
</head>
<body>
{% block content %}
{% if form.customer_choices.widget.choices == 'commercial' %}
<form action="{% url 'datacollector:step2aformview' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% else %}
<form action="{% url 'datacollector:step2bformview' %}" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
{% endif %}
{% endblock %}
</body>
</html>
forms.py
from .models import CustomerInfo, CustomerDocument
from django.forms import ModelForm
from django import forms
CUSTOMER_CHOICES = [
('commercial', 'Commercial'),
('private', 'Private'),
]
class CustomerTypeForm(forms.Form):
customer_chioces = forms.CharField(label="Customertype",
widget=forms.RadioSelect(choices=CUSTOMER_CHOICES))
#Commercial customer information
class CustomerInfoForm1(ModelForm):
class Meta:
model = CustomerInfo
fields = "__all__"
#Private customer information
class CustomerInfoForm2(ModelForm):
class Meta:
model = CustomerInfo
fields = ['name1', 'name2', 'name3', 'street', 'house', 'zip1', 'city']
class CustomerDocumentForm(forms.ModelForm):
class Meta:
model = CustomerDocument
fields = ['document']