'How do I make the view to show a multi-step form that has forms and formsets
I want to use a multi-step form combined with some formsets in Django but I realize I need more experience on the subject. I also see that there is hardly much documentation about it and what little I find they take many things for granted. Can you help me understand how to create a view that serves as a multi-step and that saves the values of the forms and formsets? previously I was already using JS for the wizard, the only thing missing is that I save the values in the database. thanks :D
example Views.py (I found this code on a site but I don't know how to adapt it with my formsets and my multiple forms, I also see that it does not explain how to accommodate the urls.py file)
def step1(request):
initial={'fn': request.session.get('fn', None)}
form = PersonForm(request.POST or None, initial=initial)
if request.method == 'POST':
if form.is_valid():
request.session['fn'] = form.cleaned_data['fn']
return HttpResponseRedirect(reverse('step2'))
return render(request, 'step1.html', {'form': form})
def step2(request):
form = PetForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
pet = form.save(commit=False)
person = Person.objects.create(fn=request.session['fn'])
pet.owner = person
pet.save()
return HttpResponseRedirect(reverse('finished'))
return render(request, 'step2.html', {'form': form})
Views.py (the code that I am doing)
def create_Presupuestos(request):
extra_forms = 1
ParteFormSet = formset_factory(PresupuestosParteForm, extra=extra_forms, max_num=20)
ManoObraFormSet = formset_factory(PresupuestosManoObraForm, extra=extra_forms, max_num=20)
PagosFormSet = formset_factory(PresupuestosPagosForm, extra=extra_forms, max_num=20)
presupuestosclientesform=PresupuestosClientesForm(request.POST or None)
presupuestosvehiculosform=PresupuestosVehiculosForm(request.POST or None)
presupuestosparteform=PresupuestosParteForm(request.POST or None)
presupuestosmanoobraform=PresupuestosManoObraForm(request.POST or None)
presupuestospagosform=PresupuestosPagosForm(request.POST or None)
presupuestosfotosform=PresupuestosFotosForm(request.POST or None)
if request.method == 'POST':
formset = ParteFormSet(request.POST, request.FILES)
manoObra_formset = ManoObraFormSet(request.POST, request.FILES,prefix='manoobra')
pagos_formset = PagosFormSet(request.POST, request.FILES, prefix='pagos')
if formset.is_valid() and manoObra_formset.is_valid() and pagos_formset.is_valid():
presupuestosclientesform.save()
return redirect('presupuestos:index')
else:
formset = ParteFormSet()
manoObra_formset = ManoObraFormSet(prefix='manoobra')
pagos_formset = PagosFormSet(prefix='pagos')
presupuestosfotosform = PresupuestosFotosForm(request.POST or None)
return render(request,'Presupuestos/new-customer.html',{
'presupuestosclientesform':presupuestosclientesform,
'presupuestosvehiculosform':presupuestosvehiculosform,
'presupuestosparteform':presupuestosparteform,
'presupuestosmanoobraform':presupuestosmanoobraform,
'presupuestospagosform':presupuestospagosform,
'presupuestosfotosform':presupuestosfotosform,
'formset':formset,
'manoObra_formset':manoObra_formset,
'pagos_formset':pagos_formset
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
