如何在 Django 的同一页面上插入 2 个不同的表单 [英] How insert 2 different forms on the same page in Django
本文介绍了如何在 Django 的同一页面上插入 2 个不同的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在同一页面中插入 2 个表单:
I have to insert 2 forms in the same page:
1) 注册表
2) 登录表单
.
所以如果我在 views.py 中使用它:
So if I use this in the views.py:
if request.method == 'POST':
form = registrationForm(request.POST)
if form.is_valid():
form.save()
return render_to_response('template.html', {
'form': form,
})
提交两种表格中的一种会出错.
I will get error by submitting one of two forms.
如何区分视图中提交的 2 个表单?
How can I distinguish the 2 forms submitting in the views ?
推荐答案
你也可以这样做,
<form method='POST'>
{{form1.as_p}}
<button type="submit" name="btnform1">Save Changes</button>
</form>
<form method='POST'>
{{form2.as_p}}
<button type="submit" name="btnform2">Save Changes</button>
</form>
代码
if request.method=='POST' and 'btnform1' in request.POST:
do something...
if request.method=='POST' and 'btnform2' in request.POST:
do something...
这篇关于如何在 Django 的同一页面上插入 2 个不同的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文