0

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.

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

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" />

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.

Community
  • 1
  • 1
Transformer
  • 3,642
  • 1
  • 22
  • 33

1 Answers1

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

Transformer
  • 3,642
  • 1
  • 22
  • 33