自定义中间件五个方法(部分方法)实例
自定义中间件项目:
模板Templates
login.html
1 {% load static %} 2 <!DOCTYPE html> 3 <html lang="en"> 4 <head> 5 <meta charset="UTF-8"> 6 <script src="{% static 'jquery-3.4.1.js' %}"></script> 7 <script src="{% static 'jquery-cookie-1.4.1.js' %}"></script> 8 <title>login</title> 9 </head> 10 <body> 11 <div> 12 用户名:<input type="text" id="username"><br> 13 密码:<input type="password" id="password"><br> 14 {% csrf_token %} 15 <input type="button" id="submit" value="登录"> 16 <span id="warning" style="color: red;"></span> 17 </div> 18 </body> 19 <script> 20 $(function () { 21 $('#submit').click(function () { 22 23 $.ajax({ 24 url:'{% url "login" %}', 25 type:'post', 26 headers:{'X-CSRFToken':$.cookie('csrftoken')}, 27 data:{ 28 username:$('#username').val(), 29 password:$('#password').val(), 30 31 }, 32 success:function (response) { 33 if (response.status){ 34 location.href=response.url 35 }else { 36 $('#warning').text('账号或密码有误!') 37 } 38 } 39 }) 40 }) 41 }) 42 </script> 43 </html>