Thanks for the reference. The problem is solved with this code
modification:
def contact(request):
if request.method == 'POST':
f = ContactForm(request.POST)
if f.is_valid():
email = f.cleaned_data['email']
... #send email
return HttpResponseRedirect(reverse('contact_success'))
else:
f=ContactForm()
return render_to_response(
'post_contact.html',
{ 'form':f },
context_instance = RequestContext(request)
)
On Dec 31, 12:36 pm, Daniel Roseman <[email protected]>
wrote:
> On Dec 31, 12:18 am, Chuck22 <[email protected]> wrote:
>
>
>
>
>
> > class ContactForm(forms.Form):
> > email = forms.EmailField(required=True,
> > widget=forms.TextInput(attrs=
> > {'size':'30'}),
> > error_messages={'required':'Please fill
> > out your Email'})
> > ...
>
> > in my form html file:
> > ...
> > label for="id_full_name">Your email address</label>
> > {{ form.email.errors }}
> > {{ form.email }}<br/>
> > ...
>
> > in my view:
> > def contact(request):
> > if request.method == 'POST':
> > f = ContactForm(request.POST)
> > if f.is_valid():
> > email = f.cleaned_data['email']
> > ...
> > return HttpResponseRedirect(reverse('contact_success'))
>
> > When user submit the contact form without fill out email field, the
> > form get submitted without displaying error message 'Please fill out
> > your Email'. Instead, I got error: The view app.views.contact didn't
> > return an HttpResponse object.
>
> > it seems f.is_valud return false, which is correct. But I think form
> > validation should kick in at this point and return error message to
> > {{ form.email.errors }} field in template. Why doesn't the validation
> > work? Did I miss anything?
>
> As Alex says, we'll need to see the full view function to really help.
> But assuming the last line what you've posted is actually the last
> line of the function, you are indeed missing something - the code to
> return something if the form fails validation.
>
> The error message you've posted tells you exactly what's happening -
> your view is not returning an HttpResponse. It's always your
> responsibility to do this - Django won't ever do something by default
> (except 404 and 500 errors). Look more closely at the example given in
> the
> documentation:http://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-...
> You'll see that in that view, the return at the end is called in both
> the case of the initial GET, and if the POST failed validation.
> --
> DR.- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---