Opened 15 years ago
Closed 15 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: | no | UI/UX: | no |
Description (last modified by )
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 by , 15 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 15 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → 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.
Fixed the formatting, in the future please use preview.