djjango cookie和session 的几种常用需求使用方法
------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 的几种常用需求使用方法的更多相关文章
- 干货:结合Scikit-learn介绍几种常用的特征选择方法
原文 http://dataunion.org/14072.html 主题 特征选择 scikit-learn 作者: Edwin Jarvis 特征选择(排序)对于数据科学家.机器学习从业者来说非 ...
- 结合Scikit-learn介绍几种常用的特征选择方法
特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解数据的特点.底层结构,这对进一步改善模型.算法都有着重要作用. 特征选择主要有两个功能: 减 ...
- [转载]Scikit-learn介绍几种常用的特征选择方法
#### [转载]原文地址:http://dataunion.org/14072.html 特征选择(排序)对于数据科学家.机器学习从业者来说非常重要.好的特征选择能够提升模型的性能,更能帮助我们理解 ...
- VxWorks嵌入式系统几种常用的延时方法
1 taskDelay taskDelay(n)使调用该函数的任务延时n个tick(内核时钟周期).该任务在指定的时间内主动放弃CPU,除了taskDelay(0)专用 于任务调度(将CPU交 ...
- 用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)
CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错 ...
- Hashtable几种常用的遍历方法
Hashtable 在System.Collection是命名空间李Hashtable是程序员经常用到的类,它以快速检索著称,是研发人员开发当中不可缺少的利器. Hashtable表示键/值对的集合, ...
- golang几种常用配置文件使用方法总结(yaml、toml、json、xml、ini)
原文连接: https://blog.csdn.net/wade3015/article/details/83351776 yaml配置文件的使用方法总结 首先介绍使用yaml配置文件,这里使用的是g ...
- 禁用cookie后session是如何设置的
我们都知道当在session 会话有基于cookie和基于url两种传递SESSIONID的方法.为了实现客户端禁止cookie发送的情况也不影响客户登陆网站,可以设置 php.ini中 sessio ...
- 转:cookie和session(一)——原理
文章来自于:http://blog.csdn.net/half1/article/details/21645545 一.cookie和session是什么? cookie是服务器留在客户端中的小文 ...
随机推荐
- linux下/proc/diskstats文件详解
每一列的含义分别为: 第一列为 设备号 (number of issued reads. This is the total number of reads completed successfull ...
- Vue + Element UI 实现权限管理系统(更换皮肤主题)
自定义主题 命令行主题工具 1.安装主题工具 首先安装「主题生成工具」,可以全局安装或者安装在当前项目下,推荐安装在项目里,方便别人 clone 项目时能直接安装依赖并启动. yarn add ele ...
- java字符串根据空格截取并存进ArrayList,并在每个元素前后加上/
public class List { public static void main(String[] args) { String s = "abc nnn ooo/xzsxc bs&q ...
- 初学Linux系统最应该做对的4件事情[长文]
“闲来无事,逛逛贴吧”已经是本人无事消磨时间的最佳选择了.五花八门的问题,各式各样的回答,总能给自己带来无限的欢乐.当然也有些问题值得自己去思考或者回答.之前就有人在贴吧里问到“Linux好难啊!该怎 ...
- [POJ2985]The k-th Largest Group
Problem 刚开始,每个数一个块. 有两个操作:0 x y 合并x,y所在的块 1 x 查询第x大的块 Solution 用并查集合并时,把原来的大小删去,加上两个块的大小和. Notice 非旋 ...
- SQL-29 使用join查询方式找出没有分类的电影id以及名称
题目描述 film表 字段 说明 film_id 电影id title 电影名称 description 电影描述信息 CREATE TABLE IF NOT EXISTS film ( film_i ...
- 在JS文件中,不需要<script>标签
在JS文件中,不需要<script>标签\
- 玩转X-CTR100 l STM32F4 l RNG硬件随机数发生器
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 STM32F4硬件随 ...
- VCL界面控件DevExpress VCL发布v18.2.2|附下载
DevExpress VCL Controls是 Devexpress公司旗下最老牌的用户界面套包.所包含的控件有:数据录入,图表,数据分析,导航,布局,网格,日程管理,样式,打印和工作流等,让您快速 ...
- Ubuntu16.04 python import cv2
有些项目源代码里面需要导入cv2,没有安装的话会出现ImportError: No module named cv2. 下面给出如何在ubuntu下安装cv2: 直接在ternimal终端中输入命令: ...