Opened 5 years ago

Closed 5 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)

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
    else:
        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: http://docs.djangoproject.com/en/1.2/topics/forms/#topics-forms-index

Change History (2)

comment:1 Changed 5 years ago by Alex

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Fixed the formatting, in the future please use preview.

comment:2 Changed 5 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from new to closed

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