[Django]-Displaying Django Form Results

2👍

After validation, the form data is found in form.cleaned_data dictionary. So you can pass that back to the template and display it as you see fit.

from django.shortcuts import render
from contracts.forms import GenerateContract

# Create your views here.
def index(request):
    return render(request, 'contracts/index.html')

def contractview(request):
    form = GenerateContract()
    if request.method == "POST":
        form = GenerateContract(request.POST)
        if form.is_valid():
            form.save(commit=True)
            return render(request,'contracts/contracts.html',{'form_data': form.cleaned_data})
        else:
            print('ERROR')
    return render(request,'contracts/contracts.html',{'form':form})

0👍

If you want to show the form with the saved values, you can render the template with form and fill the instance input . like this:

from django.shortcuts import render
from contracts.forms import GenerateContract

# Create your views here.
def index(request):
    return render(request, 'contracts/index.html')

def contractview(request):
    form = GenerateContract()
    if request.method == "POST":
        form = GenerateContract(request.POST)
        if form.is_valid():
            saved_instance = form.save(commit=True)
            return render(request,'contracts/contracts.html',{'form':GenerateContract(instance=saved_instance)})
        else:
            print('ERROR')
    return render(request,'contracts/contracts.html',{'form':form})

Leave a comment