模板中的多个表单

时间:2013-01-29 23:03:35

标签: django django-forms django-templates django-views

只是想知道将这些表单传递给单个模板并验证它们的最有效方法是什么。

class AForm(ModelForm):
    #stuff here

class BForm(ModelForm):
    #stuff here 

class CForm(ModelForm):
    #stuff here 

1 个答案:

答案 0 :(得分:1)

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)