如何在django模板中应用表单前缀

时间:2021-10-24 03:40:24

I was having a lot of trouble today with having mutiple forms in a page. my problem was having the form action to different url. it was able to different the form, so even the form.errors will know the one to display. i check here, which was where i was go but still didnt tell me how to display these forms in django template after defined them in views.py.

今天我在页面中有多个表单时遇到了很多麻烦。我的问题是对不同的URL进行表单操作。它能够使形式不同,所以即使是form.errors也会知道要显示的那个。我在这里查看,这是我去的地方,但仍然没有告诉我如何在views.py中定义它们之后如何在django模板中显示这些表单。

This was my views

这是我的看法

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))

Here is models.py

这是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)

the three models share the same field, and their forms are to be in the same page, so assuming i have {{ form.name_of_term.errors }}, i didnt know how to add the prefix of form which i displayed in html format <input type="text" name="form1-name_of_term" id="id_form1-name_of_term" />

这三个模型共享相同的字段,它们的形式将在同一页面中,所以假设我有{{form.name_of_term.errors}},我不知道如何添加以html格式显示的表单前缀< input type =“text”name =“form1-name_of_term”id =“id_form1-name_of_term”/>

I tried this {{ form.prefix }}{{ form.name_of_term }} and also read some answers like How can I build multiple submit buttons django form?, Django: Display form name in template with prefix. I had some cool answers there but it didnt help me.

我尝试了这个{{form.prefix}} {{form.name_of_term}}并阅读了一些答案,例如我如何构建多个提交按钮django表单?,Django:在带有前缀的模板中显示表单名称。我有一些很酷的答案,但它没有帮助我。

1 个解决方案

#1


0  

After all, I tried this

毕竟,我试过这个

{{ form1.name_of_term }} 

simply made it work like charm. Although i didnt know and i cant explain. I just taught that since i defined it as form1, then i should be able to retrieve the form

简单地使它像魅力一样工作。虽然我不知道,我无法解释。我刚刚教过,因为我将它定义为form1,那么我应该能够检索表单

#1


0  

After all, I tried this

毕竟,我试过这个

{{ form1.name_of_term }} 

simply made it work like charm. Although i didnt know and i cant explain. I just taught that since i defined it as form1, then i should be able to retrieve the form

简单地使它像魅力一样工作。虽然我不知道,我无法解释。我刚刚教过,因为我将它定义为form1,那么我应该能够检索表单