Check the comments on LHS of the Book page. In there is a simple method that works and does not need to remove the middleware.
Specifically (as there are loads of comments)
Frank Kruchio's comment in the comment section next to this text

Try running this locally. Load the form, submit it with none of the fields filled out, submit it with an invalid e-mail address, then finally submit it with valid data. (Of course, depending on your mail-server configuration, you might get an error when send_mail() is called, but that’s another issue.)

On 8/8/2011 8:57 AM, Hayyan Rafiq wrote:
Here is how i did it
@csrf_exempt
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', 'nore...@example.com'),
                ['siteow...@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form = ContactForm()
    return render_to_response('contact_form.html', {'form': form})


From: hayya...@hotmail.com
To: django-users@googlegroups.com
Subject: RE: Problem with django book in Forms chapter 7
Date: Sun, 7 Aug 2011 20:11:51 +0000

Hi just started facing the same problem which you did in chapter 7 . I tried using

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', 'nore...@example.com'),
                ['siteow...@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form = ContactForm()
    return render_to_response('contact_form.html', {'form': form},context_instance=RequestContext(request))

but still i get the following could you please tell me how you resolved the issue...

Forbidden (403)

CSRF verification failed. Request aborted.

Help

Reason given for failure:
    CSRF token missing or incorrect.
    
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
  • The view function uses RequestContext for the template, instead of Context.
  • In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
  • If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.


> Date: Sun, 7 Aug 2011 09:10:36 +0200
> From: rafadurancastan...@gmail.com
> To: django-users@googlegroups.com
> Subject: Re: Problem with django book in Forms chapter 7
>
> I had the same problem as you, since the book was written using an older
> django version and there was some changes on csrf for django version
> 1.2. Looking at django docs
> https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#how-to-use-it
> you can read recommended way to use this
>
>
> On 06/08/11 23:05, bob gailer wrote:
> > I love the django book. Until I got to the section "Tying Your First
> > Form Class".
> >
> > Problem:-"This class can live anywhere you want — including directly
> > in your views.py file — but community convention is to keep Form
> > classes in a separate file called forms.py. Create this file in the
> > same directory as your views.py" The examples then use from
> > contact.forms import ContactForm. Where did contact come from? I had
> > to remove it to get the import to work!
> >
> > Then all is OK until "Tying Form Objects Into Views". Here is where I
> > run into the
> > CSRF verification failed. Request aborted.
> > Reason given for failure: CSRF token missing or incorrect."
> >
> > After much searching I found:
> >
> > from django.template import RequestContext
> > ...
> > form = ContactForm()
> > return render_to_response('contact_form.html', {'form': form},
> >
> > context_instance=RequestContext(request))
> > and now it works.
> >
>
> --
> 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+unsubscr...@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+unsubscr...@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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply via email to