Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHien Ha
1,417 PointsValidility ?
Hello Why the website keep state about: " Make sure you check the form's validity and return the form with bad data to the view if it's invalid." Thanks
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from . import forms
def send_lead(email, url):
send_mail(
'Lead from {}'.format(email),
'{} submitted {}'.format(email, url),
email,
['kenneth@teamtreehouse.com']
)
def lead_form(request):
if request.method == 'POST':
form = forms.LeadShareForm()
if form.is_valid:
return HttpResponseRedirect(reverse('lead'))
return render(request, 'lead_form.html', {'form': form})
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThere are there errors to fix:
the
is_valid()
method call is missing parens-
form
is not defined if method is not 'POST'If
request.POST
isFalse
the variableform
is undefined.copy the
form
assignment you have inside the POST block to above the POST block. This givesform
a default value. -
the
form
assignment inside the POST valid block needs to referencerequest.POST
use
request.POST
as the argument to theLeadShareForm()
Edit: clarification added.
Post back if you need more help. Good luck!!!
Hien Ha
1,417 PointsHello I did not understand your answer.
"form is not defined if method is not 'POST'" : Yes, the question require me to check the method of the form
"the form assignment inside the valid block needs to reference request.POST" ?? What do you mean
I have revised the code
def lead_form(request):
if request.method == 'POST':
form = forms.LeadShareForm()
if form.is_valid():
return HttpResponseRedirect(reverse("lead")),
return render(request, 'lead_form.html', {'form': form})
But it still bummer :Make sure lead_form
returns HttpResponseRedirect(reverse("lead"))
if the form is valid
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,457 PointsSee expanded answer above.
Hien Ha
1,417 PointsHello I do not understand copy the form assignment you have inside the POST block to above the POST block. This gives form a default value.
the form assignment inside the POST valid block needs to reference request.POST
Chris Freeman
Treehouse Moderator 68,457 PointsYou need:
def lead_form(request):
form = forms.LeadShareForm() # generate a default form
if request.method == 'POST':
form = forms.LeadShareForm(request.POST) # gen form with POST data
# ...