在Django的一个页面上处理多个表单的正确方法 [英] Proper way to handle multiple forms on one page in Django

查看:76
本文介绍了在Django的一个页面上处理多个表单的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板页面期待两种形式。如果我只是使用一个表单,那么这个典型的例子就是这样:

  if request.method =='POST' 
form = AuthorForm(request.POST,)
如果form.is_valid():
form.save()
#做某事。
else:
form = AuthorForm()

如果我想使用然而,多个表单,我如何知道我只提交一个表单而不是另一个表单(即它仍然是request.POST,但我只想处理提交发生的表单)?






这是根据 expectphrase 的答案 > bannedphrase 是不同表单的提交按钮的名称,预期形式 bannedphraseform 是表单。

  if request.method =='POST':
if'bannedphrase'in request.POST:
bannedphraseform = BannedPhraseForm(request.POST,prefix = 'banned')
如果bannedphraseform.is_valid():
bannedphraseform.save()
expectedphraseform = ExpectedPhraseForm(prefix ='expected')
elif'expectedphrase'in request.POST :
expectedphraseform = ExpectedPhraseForm(request.POST,prefix ='expected')
如果expectedphraseform.is_valid():
expectedphraseform.save()
bannedphraseform = BannedPhraseForm(prefix ='banned ')
else:
bannedphraseform = BannedPhraseForm(prefix ='banned')
expectedphraseform = ExpectedPhraseForm(prefix ='expected')


解决方案

您有几个选项:


  1. 在两种表单的动作中放置不同的网址。然后,您将有两种不同的视图功能来处理两种不同的形式。


  2. 从POST数据中读取提交按钮值。您可以知道哪个提交按钮被点击:如何构建多个提交按钮django表单?



I have a template page expecting two forms. If I just use one form, things are fine as in this typical example:

if request.method == 'POST':
    form = AuthorForm(request.POST,)
    if form.is_valid():
        form.save()
        # do something.
else:
    form = AuthorForm()

If I want to work with multiple forms however, how do I let the view know that I'm submitting only one of the forms and not the other (i.e. it's still request.POST but I only want to process the form for which the submit happened)?


This is the solution based on the answer where expectedphrase and bannedphrase are the names of the submit buttons for the different forms and expectedphraseform and bannedphraseform are the forms.

if request.method == 'POST':
    if 'bannedphrase' in request.POST:
        bannedphraseform = BannedPhraseForm(request.POST, prefix='banned')
        if bannedphraseform.is_valid():
            bannedphraseform.save()
        expectedphraseform = ExpectedPhraseForm(prefix='expected')
    elif 'expectedphrase' in request.POST:
        expectedphraseform = ExpectedPhraseForm(request.POST, prefix='expected')
        if expectedphraseform.is_valid():
            expectedphraseform.save() 
        bannedphraseform = BannedPhraseForm(prefix='banned')
else:
    bannedphraseform = BannedPhraseForm(prefix='banned')
    expectedphraseform = ExpectedPhraseForm(prefix='expected')

解决方案

You have a few options:

  1. Put different URLs in the action for the two forms. Then you'll have two different view functions to deal with the two different forms.

  2. Read the submit button values from the POST data. You can tell which submit button was clicked: How can I build multiple submit buttons django form?

这篇关于在Django的一个页面上处理多个表单的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆