cookie项目文件:

  Django操作cookie实例

templates模板:

login.html 

 1 {% load static %}
 2 <!DOCTYPE html>
 3 <html lang="en">
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>login</title>
 7 </head>
 8 <body>
 9 <div>
10     用户名:<input type="text" id="username"><br>
11     密码:<input type="password" id="password"> <br>
12     <button id="submit">登录</button><pan id="warning" style="color: red"></pan>
13     {% csrf_token %}
14 </div>
15 </body>
16 <script src="{% static 'jquery-3.4.1.js' %}"></script>
17 <!--<script src="{% static 'js/login.js' %}"></script>--><!--ajax中有url的反向解析,只能放在html模板中-->
18 <script>
19  $(function () {
20         $('#submit').click(function () {
21             $.ajax({
22                 url:"{% url 'login' %}",
23                 type:'post',
24                 data:{
25                     username:$('#username').val(),
26                     password:$('#password').val(),
27                     csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),//可以直接放在headers里边
28                 },
29                 success:function (response) {
30                     if (response.status===0){
31                         //$('#submit').after('<span><i>账号或密码有误</i></span>')
32                         $('#warning').text('账号或密码有误')
33                     }else if (response.status===1){
34                         location.href=response.url
35                     }
36                 }
37             })
38         })
39     });
40 </script>
41 </html>
login.html

相关文章:

  • 2021-09-16
  • 2022-03-07
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2021-12-27
猜你喜欢
  • 2021-06-30
  • 2021-06-29
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2021-08-19
相关资源
相似解决方案