Django-website Program Case Series -1 CSRF

Source: Internet
Author: User

Django provides users with the ability to prevent cross-site request forgery

You need to configure settings.py:

Django.middleware.csrf.CsrfViewMiddleware

1. Form form submission

<form action= "/logi/" method= "POST" >    {% csrf_token%}    #需要在form表单中添加 {% csrf_token%}     <input type= "Text" name= "user"/>    <input type= "text" name= "pwd"/>    <input type= "checkbox" Name= "RMB" value= "1" /> 10 sec Free Login    <input type= "Submit" value= "Commit"/></form>

  

2. Ajax Way to submit

JS uses Ajax to submit data, adding Csrf<script src= "/static/jquery.min.js" ></script><script src= "/static/ Jquery.cookie.js "></script><script>    $ (function () {        $ (' #btn '). Click (function () {              $. Ajax ({                URL: '/logi/',                type: ' POST ',                data: {' user ': ' Root ', ' pwd ': ' 123 '},                headers: {' X-csrftoken ': $. Cookie (' Csrftoken ')},  #设置csrftoken到http响应头中 where the key is X-csrftoken is Django fixed                success:function (ARG) {                }            })        })    }) </script>

  

Django-website Program Case Series -1 CSRF

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.