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 trial

Python Django Forms Forms Validate a Form in a View

Validility ?

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

myproject/views.py
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
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

There 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 is False the variable form is undefined.

    :point_right: 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

    :point_right: use request.POST as the argument to the LeadShareForm()

Edit: clarification added.

Post back if you need more help. Good luck!!!

Hello 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]

Hello I do not understand :point_right: 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
Chris Freeman
Treehouse Moderator 68,457 Points

You 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
        # ...