Opened 8 years ago

Closed 8 years ago

#13736 closed (invalid)

possible example code error

Reported by: ikiini Owned by: nobody
Component: Documentation Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Alex Gaynor)

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,

Notice that the render_to_response uses the form variable that may not have been initialized. this is located at:

Change History (2)

comment:1 Changed 8 years ago by Alex Gaynor

Description: modified (diff)

Fixed the formatting, in the future please use preview.

comment:2 Changed 8 years ago by Karen Tracey

Resolution: invalid
Status: newclosed

I am not seeing how form could be uninitialized before use. form is initialized in both the case where request.method is POST and when request.method is anything else. (The else goes with the first if not the if form.is_valid().) One of those blocks of code must run before render_to_response is reached.

Note: See TracTickets for help on using tickets.
Back to Top