session项目文件:

 

 Django操作session实例

 

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                     console.log(response)
31                     if (response.status===0){
32                         //$('#submit').after('<span><i>账号或密码有误</i></span>')
33                         $('#warning').text('账号或密码有误')
34                     }else if (response.status===1){
35                         location.href=response.url
36                     }
37                 }
38             })
39         })
40     });
41 </script>
42 </html>
login.html

相关文章:

  • 2022-12-23
  • 2021-11-12
  • 2022-12-23
  • 2021-08-19
  • 2021-07-07
  • 2022-12-23
  • 2021-12-27
  • 2021-09-03
猜你喜欢
  • 2021-11-29
  • 2021-06-19
  • 2021-11-11
  • 2021-12-20
  • 2022-01-19
  • 2021-07-21
相关资源
相似解决方案