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 <a href="https://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/" rel="nofollow noreferrer">here</a>, 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
Here is models.py
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
I tried this
and also read some answers like <a href="https://stackoverflow.com/questions/866272/how-can-i-build-multiple-submit-buttons-django-form">How can I build multiple submit buttons django form?</a>, <a href="https://stackoverflow.com/questions/18123695/django-display-form-name-in-template-with-prefix">Django: Display form name in template with prefix</a>. I had some cool answers there but it didnt help me.
This was my views
Code:
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
Code:
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
Code:
<input type="text" name="form1-name_of_term" id="id_form1-name_of_term" />
I tried this
Code:
{{ form.prefix }}{{ form.name_of_term }}