在 Django 的页面上使用多个表单 [英] Using multiple forms on a page in Django
本文介绍了在 Django 的页面上使用多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Django 中每页使用多个表单?
How do I use more than one form per page in Django?
推荐答案
请参阅以下先前提出(和回答)的问题:
Please see the following previously asked (and answered) questions:
和
根据您的实际要求,这是我最喜欢在同一页面上处理不同模型的方式:
Depending on what you're really asking, this is my favorite way to handle different models on the same page:
if request.POST():
a_valid = formA.is_valid()
b_valid = formB.is_valid()
c_valid = formC.is_valid()
# we do this since 'and' short circuits and we want to check to whole page for form errors
if a_valid and b_valid and c_valid:
a = formA.save()
b = formB.save(commit=False)
c = formC.save(commit=False)
b.foreignkeytoA = a
b.save()
c.foreignkeytoB = b
c.save()
这篇关于在 Django 的页面上使用多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文