CSRF validation of the Django framework in Python

Source: Internet
Author: User

When the form form is submitted as a post, Django defaults to a validation mechanism CSRF validation

<form action="/day02/login/"Method="Post">    {% Csrf_token%} user name:<input type="text"Name="User">Password<input type="text"Name="pwd"> <input type="checkbox"Name="Deng"Value="1">10 sec Free Login<input type="Submit"Value="Submit"> <input type="Button"Value="Click"Id="btn"></form>

Random cookie validation must be sent in order for the Django mechanism to verify success

If you submit by using Ajax Post, 403 Forbidden verification will appear.

Workaround: Send the request header in the submission with a cookie variable, the request head cannot be underlined (note)

$.ajax ({URL:'/day02/login/', type:'POST', data:{'User':'Keke','pwd':'123'}, headers:{'X-csrftoken': $.cookie ('Csrftoken')}, Suceess:function (ARG) {}

If more than one AJAX trigger, each request a little bit of trouble, in Ajax there is a pre-trigger mechanism.

    $.ajaxsetup ({               beforesend:function (xhr,settings) {                   xhr.setrequestheader ('  X-csrftoken', $.cookie ('csrftoken');               }            );

Validation commits prior to Ajax. The parameters are fixed.

But the downside to this is that all Ajax, whether it's get or post, or any other request, will go through Ajax,

If there is a get commit, do not want it to request, in Django has the adorner this parameter to set.

CSRF validation of the Django framework in Python

Related Article

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.