• Cookie的使用
from django.shortcuts import render,redirect

def login(request):
    print("1",request.COOKIES)
    if request.method == "POST":
        name = request.POST.get("user")
        pwd = request.POST.get("pwd")
        if name == "dongfei" and pwd == "dongfei":
            ret = redirect("/index")
            ret.set_cookie("id",'14bae99719abcf1f89f5',max_age=10)  #设置cookie的超时时间
            return ret
    return render(request,"login.html",locals())

def index(request):
    if request.COOKIES.get("id",None) == '14bae99719abcf1f89f5':
        name = "dongfei"
        return render(request,"index.html",locals())
    else:
        return redirect("/login/")
  • Cookie和Session的使用
from django.shortcuts import render,redirect

def login(request):
    print("COOKIES",request.COOKIES)
    print("SESSION",request.session)
    if request.method == "POST":
        name = request.POST.get("user")
        pwd = request.POST.get("pwd")
        if name == "dongfei" and pwd == "dongfei":
            # ret = redirect("/index")
            # ret.set_cookie("id",'14bae99719abcf1f89f5',max_age=10)  #设置cookie的超时时间
            # return ret
            request.session["is_login"] = True  #设置session,需要migrate数据库
            request.session["user"] = name
            # del request.session["user"]  #删除session
            # request.session.set_expiry(10)  #设置session超时时间
            return redirect("/index/")
    return render(request,"login.html",locals())

def index(request):
    # if request.COOKIES.get("id",None) == '14bae99719abcf1f89f5':
    #     name = "dongfei"
    #     return render(request,"index.html",locals())
    if request.session.get("is_login",None):
        name = request.session.get("user")
        return render(request,"index.html",locals())
    else:
        return redirect("/login/")

191107Django的Cookie和Session的更多相关文章

  1. Cookie和Session的总结

    1.开篇 在之前学习这一段的时候我一直有点没弄清楚,其实对Session这块的理解还可以,但是Cookie感觉始终还是欠缺点火候.之后的很长一段时间都基本上很少用Cookie了,渐渐的也淡忘了这一块的 ...

  2. java的会话管理:Cookie和Session

    java的会话管理:Cookie和Session 1.什么是会话 此处的是指客户端(浏览器)和服务端之间的数据传输.例如用户登录,购物车等 会话管理就是管理浏览器客户端和服务端之间会话过程产生的会话数 ...

  3. Cookie和Session的那些事儿

    Cookie和Session都是为了保持用户的访问状态,一方面为了方便业务实现,另一方面为了简化服务端的程序设计,提高访问性能.Cookie是客户端(也就是浏览器端)的技术,设置了Cookie之后,每 ...

  4. django的cookie和session以及内置信号、缓存

    cookie和session cookie和session的作用: cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话.两者最大的区别是cookie的信息是存放在浏览 ...

  5. Cookie和Session的区别

    前言 HTTP是一种无状态的协议,为了分辨链接是谁发起的,就需要我们自己去解决这个问题.不然有些情况下即使是同一个网站我们每打开一个页面也都要登录一下.而Session和Cookie就是为解决这个问题 ...

  6. 本地数据Store。Cookie,Session,Cache的理解。Timer类主要用于定时性、周期性任务 的触发。刷新Store,Panel

    本地数据Store var monthStore = Ext.create('Ext.data.Store', { storeId : 'monthStore', autoLoad : false, ...

  7. Cookie与Session

    再说Cookie与Session之前,先要了解一下http协议. 何为http协议: http协议即超文本传输协议,一种基于浏览器请求与服务器响应的协议,该协议主要的特点就是它是一种无状态的协议(只针 ...

  8. 【转】Cookie和Session区别和联系详解

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  9. 理解Cookie和Session机制(转)

    目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...

随机推荐

  1. nodejs---crypto模块MD5签名

    1.MD5是一种常用的哈希算法,用于给任意数据一个“签名”.这个签名通常用一个十六进制的字符串表示: /*md5签名*/ /*引入crypto模块*/ const crypto = require(' ...

  2. 对ArrayList中的Person对象按照先年龄从大到小,相同年龄的再按照姓名(姓名是英文的)的字母顺序进行排序.

    ListDemo2.java ----------------- package com.fs.test; import java.util.ArrayList; import java.util.C ...

  3. Windows访问VirtualBox的Redis服务器

    一般来讲,我们不愿意在Windows上面安装太多的软件,这样会导致Windows运行太慢. 所以我在windows上面安装了VirtualBox,然后把相关的软件都安装在virtualBox里面,比如 ...

  4. Delphi ListBox组件

  5. 【异常】org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=36, exceptions:

    1 Phoenix远程无法连接但是本地可以连接,详细异常 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found bindin ...

  6. 异常-User class threw exception: java.lang.IllegalStateException: Cannot call methods on a stopped SparkContext.

    1 详细信息 User class threw exception: java.lang.IllegalStateException: Cannot call methods on a stopped ...

  7. linux pip使用国内源

    最近在Linux里面使用pip安装应用的速度十分的慢,于是便上网找了一些国内的源. 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:https:// ...

  8. Maven 技巧知多少

    Maven是一种帮助我们快速构建项目的小工具,它可以解决我们在项目过程中手动导包造成的版本不一致的问题,以及找包困难等问题,同时通过Maven创建的项目都有固定的目录格式,使得约定优于配置,我们通过固 ...

  9. .htaccess 一段神奇的跳转代码

    <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_REFERER} ^.*(google|ask|yahoo|you ...

  10. 【AGC 036C】GP2

    https://atcoder.jp/contests/agc036/tasks/agc036_c 题意 有一个长度为 $n$ 的非负整数序列 $x$,初始时全为 $0$.一次操作定义为选择一对正整数 ...