在Django中从单个页面处理不同表单的最佳方法是什么?

时间:2010-08-25 13:59:56

标签: django django-forms

假设我在视图中定义了3种不同的形式:

# views.py
form_one = FormOne()
form_two = FormTwo()
form_three = FormThree()

在我的模板中:

<form action="" method="post" id="form-one">
  {{ form_one.as_table }}
  <input type="submit" value="Submit Form One" name="form-one" />
</form>

<form action="" method="post" id="form-two">
  {{ form_two.as_table }}
  <input type="submit" value="Submit Form Two" name="form-two" />
</form>

<form action="" method="post" id="form-three">
  {{ form_three.as_table }}
  <input type="submit" value="Submit Form Three" name="form-three" />
</form>

假设每个表单都有自己唯一的字段名称,如何从一个视图处理所有3个表单?我在考虑以下方法,但我不确定这是否是解决此问题的最佳方法:

# views.py

if request.method == 'POST':
    request_post = request.POST
    if 'form-one' in request_post:
        form_one = FormOne(request.POST)
    elif 'form-two' in request_post:
        form_two = FormTwo(request.POST)
    else:
        form_three = FormThree(request.POST)
else:
    form_one = FormOne()
    form_two = FormTwo()
    form_three = FormThree()

有任何意见或建议吗?

3 个答案:

答案 0 :(得分:4)

处理多个表单的正确方法是在创建表单时使用“prefix”属性。这就是您的视图的样子:

if request.method == 'POST':
    form1 = Form1(request.POST, prefix='form1')
    form2 = Form2(request.POST, prefix='form2')
    form3 = Form3(request.POST, prefix='form3')

    if form1.is_valid() and form2.is_valid() and form3.is_valid():
        # Do whatever you have to do
        pass

else:
    form1 = Form1(prefix='form1')
    form2 = Form2(prefix='form2')
    form3 = Form3(prefix='form3')

模板保持不变,不需要额外的逻辑:

<form ...>
    {{ form1.as_table }}
    {{ form2.as_table }}
    {{ form3.as_table }}

    <input type="submit" ... />
</form>

答案 1 :(得分:3)

  1. 我的第一个想法是你可以为每个表单操作属性添加一个'?form_id = 1'。

    <form action="?form_id=1" method="post" id="form-one">
    

    在视图中:

    form_id = request.GET.get('form_id', None)
    if form_id == '1':
        form_one = FormOne(request.POST)
    
  2. 另一种选择是创建单独的网址发布到。

    在urls.py中

    url(r'^form1/$', 'someapp.views.process_forms', {'form_class': FormOne}, name='form_one_page'),
    url(r'^form2/$', 'someapp.views.process_forms', {'form_class': FormTwo}, name='form_one_page'),
    url(r'^form3/$', 'someapp.views.process_forms', {'form_class': FormThree}, name='form_one_page'),
    
    在views.py中

    def process_forms(request, form_class=None):
        ...
        form = form_class(data=request.POST)
        ...
    
  3. 您可以检查提交按钮名称,因为您在提交按钮中使用它们。

    if request.POST.has_key('form-one'):
        ...
    elif request.POST.has_key('form-two'):
        ...
    elif request.POST.has_key('form-three'):
        ...
    else:
        ...
    

答案 2 :(得分:0)

我在一个正在处理的网站上遇到了类似的问题。我现在没有代码,但我认为我在这些方面做了一些事情:

if request.method == 'POST': 
    valid = False
    form = FormOne(request.POST) 
    if form.is_valid():
        #handle case where use submitted FormOne
        valid = True

    form = FormTwo(request.POST) 
    if form.is_valid():
        #handle case where use submitted FormTwo
        valid = True

    form = FormThree(request.POST) 
    if form.is_valid():
        #handle case where use submitted FormThree
        valid = True

    if not valid:
        #handle case where none of the forms were valid