今天我在网页上有多个表单时遇到了很多麻烦。我的问题是对不同的URL进行表单操作。它能够使形式不同,所以即使是form.errors也会知道要显示的那个。我检查here,这是我去的地方,但仍然没有告诉我如何在views.py中定义它们之后如何在django模板中显示这些表格。
这是我的观点
def TermAdd(request):
if request.method == 'POST':
form1 = SchoolFirstTermForm(request.POST, prefix="form1", request=request)
form2 = SchoolSecondTermForm(request.POST, prefix="form2", request=request)
form3 = SchoolThirdTermForm(request.POST, prefix="form3", request=request)
if form1.is_valid() and form2.is_valid() and form3.is_valid():
if 'term_add_button' in request.POST:
myterm = form1.save(commit=False)
myterm.session = session_dates
myterm.save()
messages.add_message(request, messages.SUCCESS, ("Administrator %s, The %s for %s Sessions were added successfully") % (request.user, myterm.name_of_term, session_dates.current_session))
return HttpResponseRedirect(reverse('school_session.views.SchoolTermAddView'))
else:
form1 = SchoolFirstTermForm(prefix="form1")
form2 = SchoolSecondTermForm(prefix="form2")
form3 = SchoolThirdTermForm(prefix="form3")
context = {'form1':form1, 'form2':form2, 'form3':form3, 'session_dates':session_dates, 'term_dates':term_dates}
return render_to_response('schools/admin_term_add.html', context, context_instance=RequestContext(request))
这是models.py
class SchoolFirstTerm(models.Model):
session = models.ForeignKey(SchoolSession)
name_of_term = models.CharField(verbose_name="Name of Term", max_length=15, help_text="leave this except you want to change the name of your First Term to another word")
term_start = models.DateField()
term_end = models.DateField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return "%s Term of %d/%d session" % (self.name_of_term, self.session.current_session)
class SchoolSecondTerm(models.Model):
session = models.ForeignKey(SchoolSession)
name_of_term = models.CharField(verbose_name="Name of Term", max_length=15, help_text="leave this except you want to change the name of your First Term to another word")
term_start = models.DateField()
term_end = models.DateField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return "%s Term of %d/%d session" % (self.name_of_term, self.session.current_session)
class SchoolThirdTerm(models.Model):
session = models.ForeignKey(SchoolSession)
name_of_term = models.CharField(verbose_name="Name of Term", max_length=15, help_text="leave this except you want to change the name of your First Term to another word")
term_end = models.DateField()
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return "%s Term of %d/%d session" % (self.name_of_term, self.session.current_session)
三个模型共享相同的字段,它们的表单将在同一页面中,所以假设我有{{form.name_of_term.errors}},我不知道如何添加我显示的表单前缀html格式<input type="text" name="form1-name_of_term" id="id_form1-name_of_term" />
我尝试了这个{{ form.prefix }}{{ form.name_of_term }}
并阅读了一些答案,例如How can I build multiple submit buttons django form?,Django: Display form name in template with prefix。我有一些很酷的答案,但它没有帮助我。
答案 0 :(得分:0)
毕竟,我试过这个
{{ form1.name_of_term }}
简单地使它像魅力一样工作。虽然我不知道,我无法解释。我刚刚教过,因为我将其定义为form1
,那么我应该能够检索表格