------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. Google Protocol Buffers 反序列化 转

    http://www.cnblogs.com/royenhome/archive/2010/10/30/1865256.html   本文作为结束篇,会稍微介绍下怎么反序列化GoogleBuffer数 ...

  2. matlab作图 latex插图

    推荐用saveas eps,再用eps2pdf转成pdf.这样可以之间pdflatex编译. if result.savepic saveas(gcf,[ pwd '/picture/right_' ...

  3. 派生类时使用private的目的 《私有派生》

    第一:继承方式是public的情况下: 当成员是public的时候,派生类对象可以直接调用基类的这个方法和数据, 当数据是private的时候,派生类的对象不能直接调用之,可以通过调用基类的方法来访问 ...

  4. 分析图第二讲导出图片和后期PS5.12

    导出渲染的白模加上EXTRATEX.再导出一张“消隐”样式的模型图片.就是线稿图. 再导出一张着色显示图,并去掉边线.,用于后期PS选择范围用. 把这几张图全都导入ps. 渲染图的阴影面是灰色的,示例 ...

  5. mybatis输出sql语句

    方法一: 这种方法是mybatis官网上介绍的,比较好用: log4j.properties: log4j.rootLogger=ERROR,consolelog4j.appender.console ...

  6. linux-xshell同时向多台服务器一起发命令

    概述:有时候我们要往多台linux服务器上面步东西,一台一台布能烦死我们.如果能同时向多台服务器发命令岂不美哉. 开工: 首先打开exshell,查看->撰写栏  打开 然后瓷砖排序,看起来方便 ...

  7. Hive/Hbase/Sqoop的安装教程

    Hive/Hbase/Sqoop的安装教程 HIVE INSTALL 1.下载安装包:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.3 ...

  8. vue-12-渲染函数 & JSX

    render() Vue.component('anchored-heading', { render: function (createElement) { return createElement ...

  9. [转]如何远程连接运行OpenGL/Cuda 等GPU程序

    发现一篇神文,解决了困扰许久的远程桌面OpenGL/GPU 等问题... 原地址在这:http://www.tanglei.name/how-to-run-gpu-programs-using-rem ...

  10. java动手动脑1

    一.以下代码的输出结果是什么? int X=100; int Y=200; System.out.println("X+Y="+X+Y); System.out.println(X ...