user1940979
user1940979

Reputation: 935

Multiple forms in a template

Just wondering what is the most efficient way to pass these forms to a single template and validate them.

class AForm(ModelForm):
    #stuff here

class BForm(ModelForm):
    #stuff here 

class CForm(ModelForm):
    #stuff here 

Upvotes: 0

Views: 131

Answers (1)

msc
msc

Reputation: 3800

from django.template import RequestContext
from django.shortcuts import render_to_response
from .forms import AForm, BForm, CForm

def some_view(request):
    if request.method == 'POST':
        a_form = AForm(request.POST, request.FILES)
        b_form = BForm(request.POST, request.FILES)
        c_form = CForm(request.POST, request.FILES)

        if a_form.is_valid():
            #do stuff...
        if b_form.is_valid():
            #do stuff...
        if c_form.is_valid():
            #do_stuff...
    else:
        a_form = AForm()
        b_form = BForm()
        c_form = CForm()
    variables = RequestContext(request, {'a_form': a_form, 'b_form': b_form, 'c_form': c_form})
    return render_to_response('page.html', variables)

Upvotes: 1

Related Questions