我是django的初学者。我尝试在一个页面上实现多个表单。我成功地打印了表格,我有一个问题。我只填写对应于button4的表单,即crawl_attri,然后单击button4,它在其他字段上显示需要以下字段的错误。我应该做什么改变,以便它只显示与我点击的按钮相对应的错误....
views.py
from django.shortcuts import render,render_to_response, RequestContext
from django.http import HttpResponseRedirect
from .forms import *
from selenium import webdriver
import MySQLdb
def my_view(request):
form = first_form(request.POST or None)
preset_form = websiteform(request.POST or None)
crawl_attri=crawl_attribute(request.POST or None)
crawl_css=crawl_url_attribute_css_sel(request.POST or None)
prod=prodid(request.POST or None)
if request.method=="POST":
if preset_form.is_valid() and 'button2' in request.POST:
save_it=preset_form.save(commit=False)
save_it.save()
return HttpResponseRedirect('')
elif 'button1' in request.POST and prod.is_valid():
save_it=prod.save(commit=False)
save_it.save()
return HttpResponseRedirect('')
elif form.is_valid() and 'button3' in request.POST:
save_it=form.save(commit=False)
save_it.save()
return HttpResponseRedirect('')
elif crawl_attri.is_valid() and 'button4' in request.POST:
save_it=crawl_attri.save(commit=False)
save_it.save()
return HttpResponseRedirect('')
elif crawl_css.is_valid() and 'button5' in request.POST:
save_it=crawl_css.save(commit=False)
save_it.save()
return HttpResponseRedirect('')
return render_to_response('signup.html',{'preset_form': preset_form,'form':form,
'crawl_attri':crawl_attri,'crawl_css':crawl_css,'prod':prod},RequestContext(request))
答案 0 :(得分:0)
您应该在if块中实例化表单。例如,如果button1存在,则只将request.POST传递给preset_form。