我在頁面中使用多個表單時遇到了很多麻煩。我的問題是有不同的網址形式的行動。它能夠不同的形式,所以即使form.errors會知道要顯示的。我檢查here,這是我去的地方,但仍然沒有告訴我如何在view.py中定義它們之後在django模板中顯示這些表單。如何在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。我有一些很酷的答案,但它並沒有幫助我。