zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    稳扎稳打Silverlight(13) 2.0交互之鼠标事件和键盘事件
    稳扎稳打Silverlight(17) 2.0数据之详解DataGrid, 绑定数据到ListBox
    再接再厉VS 2008 sp1 + .NET 3.5 sp1(2) Entity Framework(实体框架)之详解 Linq To Entities 之一
    稳扎稳打Silverlight(8) 2.0图形之基类System.Windows.Shapes.Shape
    稳扎稳打Silverlight(11) 2.0动画之ColorAnimation, DoubleAnimation, PointAnimation, 内插关键帧动画
    稳扎稳打Silverlight(21) 2.0通信之WebRequest和WebResponse, 对指定的URI发出请求以及接收响应
    稳扎稳打Silverlight(16) 2.0数据之独立存储(Isolated Storage)
    稳扎稳打Silverlight(9) 2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush
    稳扎稳打Silverlight(23) 2.0通信之调用WCF的双向通信(Duplex Service)
    游戏人生Silverlight(1) 七彩俄罗斯方块[Silverlight 2.0(c#)]
  • 原文地址:https://www.cnblogs.com/open-yang/p/11222481.html
Copyright © 2011-2022 走看看