"render_to_response" instruction should be outside the "else"
statement (in the view code).
Lucian
On Thu, May 26, 2011 at 1:18 PM, Nikhil Somaru <nsomaru@gmail.com> wrote:
> Hi all,
>
> I am testing the form validation. When I try and submit an empty form, I
> get:
>
> ValueError at /contact/
>
> The view djangobook.contact.views.contact didn't return an HttpResponse
> object.
>
>
>
> # ---------------------------------- contact.views:
> ---------------------------------------------------#
>
> from django.core.mail import send_mail
> from django.template import RequestContext
> from django.shortcuts import render_to_response
> from django.http import HttpResponseRedirect
> from djangobook.contact.forms import ContactForm
>
> def contact(request):
> if request.method == 'POST':
> form = ContactForm(request.POST)
> if form.is_valid():
> cd = form.cleaned_data
> send_mail(
> cd['subject'],
> cd['message'],
> cd.get('email', 'nsomaru@gmail.com'),
> ['nsomaru@gmail.com'],
> fail_silently=False,
> )
> return HttpResponseRedirect('/contact/thanks')
> else:
> form = ContactForm()
> return render_to_response('books/contact_form.html',
> RequestContext(request, {'form': form})) #
> Required for CSRF?
>
> # ---------------------------------- forms.py:
> ---------------------------------------------------#
> from django import forms
>
> class ContactForm(forms.Form):
> subject = forms.CharField()
> email = forms.EmailField(required=False)
> message = forms.CharField()
>
>
> # ---------------------------------- books/contact_form.html:
> ---------------------------------------------------#
> <html>
> <head>
> <title>Contact Us</title>
> </head>
> <body>
> <h1>Contact Us</h1>
> {% if form.errors %}
> <p style="color: red;">
> Please correct the error{{ form.errors|pluralize }} below.
> </p>
> {% endif %}
> <form action="" method="post">{% csrf_token %}
> <table>
> {{ form.as_table }}
> </table>
> <input type="submit" value="Submit" />
> </form>
> </body>
> </html>
> #
> ------------------------------------------------------------------------------------------------------------------------------------#
>
> Could anyone explain:
> 1) why the form is not validating
> 2) How exactly the errors will be displayed?
>
>
> --
> Yours,
> Nikhil Somaru
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment