Django error-reason given for failure: csrf cookie not set.

Source: Internet
Author: User

Exercise the submission of Django form today. When the form is submitted

Forbidden (403)

Csrf Verification Failed. Request aborted.

Help

Reason given for failure:

    CSRF cookie not set.    

In general, this can occur when there is a genuine cross site request forgery, or when

Django's csrf mechanic has not been used correctly. For post forms, you need to ensure:

  • Your browser is accepting cookies.
  • The view function uses
    RequestContextFor the template, insteadContext.
  • In the template, there is{% csrf_token %}Template tag inside each post form that targets an internal URL.
  • If you are not usingCsrfViewMiddleware, Then you must use
    csrf_protect
    On any views that usecsrf_tokenTemplate tag, as well as those that accept the post data.

You're seeing the help section of this page because you haveDEBUG = TrueIn your Django settings file. change that
False, And only the initial error message will be displayed.

You can customize this page using the csrf_failure_view setting.

According to many people

Add the following content to middleware_classes in settings. py:

'Django. Middleware. csrf. csrfresponsemiddleware ',

However, my problem persists, but many people say that their problem is solved in this way. Later I learned that they used version 1.2, while I used version 1.4,

Go to the official website:

Https://docs.djangoproject.com/en/1.2/ref/contrib/csrf/

Refer to it:

To enable csrf protection for your views, follow these steps:

  1. Add the Middleware'Django. Middleware. csrf. csrfviewmiddleware'To your list ofmiddleware classes,

    Middleware_classes. (IT shoshould comebefore
    CsrfresponsemiddlewareIf that is being used, and before anyview middleware that assume that csrf attacks have been dealt .)

    Alternatively, you can use the decoratorDjango. Views. decorators. csrf. csrf_protectOn particle views youwant to protect (see below ).

  2. In any template that uses a post form, use
    Csrf_tokenTag insidethe
    <Form>Element if the form is for an internal URL, e.g .:

    <form action="" method="post">{% csrf_token %}

    This shoshould not be done for post forms that target external URLs, sincethat wocould cause the csrf token to be leaked, leading to a vulnerability.

  3. In the corresponding view functions, ensure that'Django. Core. context_processors.csrf'Context processor isbeing used. Usually, this can be done in one of two ways:

    1. Use requestcontext, which always uses'Django. Core. context_processors.csrf'(No matter what yourtemplate_context_processors setting). If you are usinggeneric views or contrib apps, you
      Are covered already, since theseapps use requestcontext throughout.

    2. Manually import and use the processor to generate the csrf token andadd it to the template context. e.g .:

      from django.core.context_processors import csrffrom django.shortcuts import render_to_responsedef my_view(request):    c = {}    c.update(csrf(request))    # ... view code here    return render_to_response("a_template.html", c)

      You may want to write your ownRender_to_responseWrapper thattakes care of this step for you.

The utility scriptExtras/csrf_migration_helper.pyCan help to automate thefinding of code and templates that may need to be upgraded. It contains fullhelp on how to use it.

But the problem persists. Later I saw another method on this website:

O manually exclude a view function from being handled by either of the two csrfmiddleware, you can use
Csrf_exemptDecorator, found inDjango. Views. decorators. csrfModule. For example:

from django.views.decorators.csrf import csrf_exempt@csrf_exemptdef my_view(request):    return HttpResponse('Hello world')

Like the middleware,Csrf_exemptDecorator is composed of two parts:Csrf_view_exemptDecorator and
Csrf_response_exemptDecorator, foundin the same module. These disable the view protection Mechanic (Csrfviewmiddleware) And the response post-processing (Csrfresponsemiddleware)
Respectively. They can be used individually ifrequired.

The problem was finally solved.

In fact, I bypassed this problem because Django introduced csrf to avoid cross site request forgeries attacks, and the preceding solution just disables this Django function. Therefore, we have to study it carefully in the future and submit the form successfully without the help of this function.

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.