[Answered ]-Confirmation form step2.html Django

1👍

Good solution will be to show step2.html in step1 view when valid form is submitted, instead of redirecting user to step2. That way you will have access to your form data in view and template.

When submitting confirmation, values from step 1 can be passed by hidden fields or saved into session storage.

Example:

def step1(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            ctx = {'contact_data': form.cleaned_data}
            return render(request, 'step2.html', ctx)
    else:
        form = ContactForm()
    return render(request, 'step1.html', {'form': form})

1👍

You can save whole form data (cleaned_data) into session storage before redirection in step 1. That way you will be able to retrieve that data in step 2. Example:

def step1(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            #save and cleared_form
            request.session.contact_form = form.cleaned_data
            return HttpResponseRedirect('/step2/')
    else:
        form = ContactForm()
    return render(request, 'step1.html', {'form': form})
def step2(request):
    contact_data = request.session.get('contact_form', None)

    if contact_data is None:
        return HttpResponseRedirect('/step1/')
        # someone is entering step 2 directly, without submitted form in step 1, we should redirect him back to step 1.

    ctx = {'contact_data': contact_data}
    return render(request, 'step2.html', ctx)

0👍

Consider using Form wizard. It will handle for you passing submitted data between steps (using cookies or session). All you need to do is: create 2 views, one with proper form, one with just some confirmation button and in template for step 2 retrieve all data from step 1.

Leave a comment