------https://www.cnblogs.com/liuqingzheng/articles/8990027.html

需求情形一:正常设置cookie

set_cookie(key,value,max_age,expires,path,domain,secure,httponly)
def test_cookie_login(request):
if request.method == 'POST':
name = request.POST.get('name')
pwd = request.POST.get('pwd')
if name == 'wudi' and pwd == '':
obj = redirect('index')
obj.set_cookie('is_login',True)
obj.set_cookie('name',name)
return obj
return render(request,'test.html') def index(request):
print(request.COOKIES)
is_login = request.COOKIES.get('is_login')
if is_login:
return render(request,'index.html',{'name':name})
else:
return redirect('/test_cookie_login')

删除cookie

@login_auth
def index(request):
name = request.COOKIES.get('name')
obj.render(request,'index.html',{'name':name})
obj.delete_cookie('is_login')
return obj

需求情形二:设置cookie在装饰器里

def login_auth(func):
def inner(request,*args,**kwargs):
is_login = request.COOKIES.get('is_login')
if is_login:
return func(request,*args,**kwargs)
else:
return redirect('/test_cookie_login/')
return inner @login_auth
def index(request):
name = request.COOKIES.get('name')
return render(request,'index.html',{'name':name}) def test_cookie_login(request):
if request.method == 'POST':
name = request.POST.get('name')
pwd = request.POST.get('pwd')
if name == 'wudi' and pwd == '':
obj = redirect('index')
obj.set_cookie('is_login',True)
obj.set_cookie('name',name)
return obj
return render(request,'test.html')

需求情形三:为了使得已经键入登录index.html但却未登录,登录成功之后直接跳转到index.html,设置cookie里加入URL

def login_auth(func):
def inner(request,*args,**kwargs):
is_login = request.COOKIES.get('is_login')
#为了使得已经键入登录index.html但却未登录,登录成功之后直接跳转到index.html
next_url = request.get_full_path()
if is_login:
return func(request,*args,**kwargs)
else:
return redirect('/test_cookie_login/?next=%s'%next_url)
return inner @login_auth
def index(request):
name = request.COOKIES.get('name')
return render(request,'index.html',{'name':name}) def test_cookie_login(request):
if request.method == 'POST':
name = request.POST.get('name')
pwd = request.POST.get('pwd')
# 为了使得已经键入登录index.html但却未登录,登录成功之后直接跳转到index.html
url = request.GET.get('next')
if name == 'wudi' and pwd == '':
obj = redirect(url)
obj.set_cookie('is_login',True)
obj.set_cookie('name',name)
return obj
return render(request,'test.html')

djjango cookie和session 的几种常用需求使用方法的更多相关文章

  1. 干货:结合Scikit-learn介绍几种常用的特征选择方法

    原文  http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...

  2. 结合Scikit-learn介绍几种常用的特征选择方法

    特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解数据的特点.底层结构,这对进一步改善模型.算法都有着重要作用. 特征选择主要有两个功能: 减 ...

  3. [转载]Scikit-learn介绍几种常用的特征选择方法

    #### [转载]原文地址:http://dataunion.org/14072.html 特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解 ...

  4. VxWorks嵌入式系统几种常用的延时方法

    1 taskDelay     taskDelay(n)使调用该函数的任务延时n个tick(内核时钟周期).该任务在指定的时间内主动放弃CPU,除了taskDelay(0)专用 于任务调度(将CPU交 ...

  5. 用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)

    CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错 ...

  6. Hashtable几种常用的遍历方法

    Hashtable 在System.Collection是命名空间李Hashtable是程序员经常用到的类,它以快速检索著称,是研发人员开发当中不可缺少的利器. Hashtable表示键/值对的集合, ...

  7. golang几种常用配置文件使用方法总结(yaml、toml、json、xml、ini)

    原文连接: https://blog.csdn.net/wade3015/article/details/83351776 yaml配置文件的使用方法总结 首先介绍使用yaml配置文件,这里使用的是g ...

  8. 禁用cookie后session是如何设置的

    我们都知道当在session 会话有基于cookie和基于url两种传递SESSIONID的方法.为了实现客户端禁止cookie发送的情况也不影响客户登陆网站,可以设置 php.ini中 sessio ...

  9. 转:cookie和session(一)——原理

    文章来自于:http://blog.csdn.net/half1/article/details/21645545 一.cookie和session是什么?   cookie是服务器留在客户端中的小文 ...

随机推荐

  1. C++11之 std::atomic (不用锁实现线程互斥)

    std::atomic_flag std::atomic_flag是一个原子的布尔类型,可支持两种原子操作: test_and_set, 如果atomic_flag对象被设置,则返回true; 如果a ...

  2. 微信小程序code 换取 session_key

    code 换取 session_key ​这是一个 HTTPS 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid.其中 session_key 是对用户数据进行 ...

  3. learning ddr input clock frequency change condition

  4. python 学习 面向对象编程

    面向对象编程---oop,是一种编程思想,oop把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行为了简化程序设计 ...

  5. sql语句最后一行显示统计。

    SELECT id, username, id_Num FROM users UNION ALL SELECT '合计', count(*), null FROM users ORDER BY id_ ...

  6. CMOS集成门电路

    CMOS集成门电路:mos管构成的集成门电路 CMOS:互补对称金属氧化物半导体器件 CMOS反相器电路是由N沟道MOSFET和P沟道MOSFET互补而成:特点静态功耗近视为0,电源电压可在很宽的范围 ...

  7. python常用内建模块 collections,bs64,struct,hashlib,itertools,contextlib,xml

    #  2  collections 是Python内建的一个集合模块,提供了许多有用的集合类. # 2.1 namedtuple #tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p ...

  8. Driver 01 进程隐藏

    大二时候的代码以及笔记,当时暂时记录在QQ上在,现在发出来分享一下. 为了写驱动装一大堆的软件插件啥的,还常常失败. 这里就顺带总结下SDK下载和WinDbg symbol路径设置正确WinDbg却总 ...

  9. Centos7安装RabbitMQ解决Erlang依赖报错

    通过yum等软件仓库都可以直接安装RabbitMQ,但版本一般都较为保守. RabbitMQ官网提供了新版的rpm包(http://www.rabbitmq.com/download.html),但是 ...

  10. 监控中的TP50

    TP指标: TP50:指在一个时间段内(如5分钟),统计该方法每次调用所消耗的时间,并将这些时间按从小到大的顺序进行排序,取第50%的那个值作为TP50 值:配置此监控指标对应的报警阀值后,需要保证在 ...