Django操作cookie实例

 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

  index.html 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>index</title>
 6 </head>
 7 <body>
 8 <div>
 9     <h1>欢迎来到首页</h1>
10 </div>
11 <a href='{% url 'more' %}'>more</a>
12 </body>
13 </html>
index.html

  more.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>more</title>
 6 </head>
 7 <body>
 8 <div><h1>更多信息</h1></div>
 9 </body>
10 </html>
more.html

viwes.py 

 1 from django.shortcuts import render, HttpResponse, redirect
 2 from django.urls import reverse
 3 from django.http import JsonResponse
 4  
 5 #cookie的使用,更多的用session
 6  
 7 def login(request):
 8     if request.method == 'GET':
 9         return render(request, 'login.html')
10     elif request.method == 'POST':
11         name = request.POST.get('username')
12         psd = request.POST.get('password')
13  
14         if name == 'yang' and psd == '123':
15             dic = {'status': 1, 'url': reverse('index')}
16             rep = JsonResponse(dic)
17             rep.set_cookie('login', True)
18             rep.set_cookie('name', name)
19         else:
20             dic = {'status': 0, 'url': reverse('login')}
21             rep = JsonResponse(dic)
22         return rep
23  
24  
25  
26 #(1)普通视图函数内部检验登录状态
27 '''
28 #登陆成功之后才能访问主页
29 def index(request):
30     # print(request.COOKIES)
31     if request.COOKIES.get('login'):
32         return render(request, 'index.html')
33     else:
34         return redirect('login')
35  
36 # 登录成功之后才能访问
37 def more(request):
38     if request.COOKIES.get('login'):
39         return render(request, 'more.html')
40     else:
41         return redirect('login')
42 '''
43  
44 #(2)用装饰器实现状态检验
45  
46 # 登录状态认证装饰函数
47 def login_auth(func):
48     def inner(request):
49         if request.COOKIES.get('login'):
50             return func(request)
51         else:
52             return redirect('login')
53     return inner
54  
55 @login_auth
56 def index(request):
57     return render(request, 'index.html')
58  
59 @login_auth
60 def more(request):
61     return render(request, 'more.html')
views.py

urls.py 

 1 from django.conf.urls import url
 2 from django.contrib import admin
 3 from app01 import views
 4  
 5 urlpatterns = [
 6     url(r'^admin/', admin.site.urls),
 7     url(r'^login', views.login, name='login'),
 8     url(r'^index/', views.index, name='index'),
 9     url(r'^more/', views.more, name='more'),
10 ]
urls.py
posted @ 2019-07-21 20:04  笑得好美  阅读(409)  评论(0编辑  收藏  举报