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是服务器留在客户端中的小文 ...
随机推荐
- java 数据溢出和编译错误的差别
int a=100000000000;编译错误,超出int范围 int a=2100000000; int b=a*12020200;数据溢出,a并未溢出,但b在通过a计算后的数据溢出 long e= ...
- 给msde加装企业管理器
-=给msde加装企业管理器=- 首先,反对所谓的绿色版,运行那是 相~~~当 不稳定,自动关闭,要你有什么用?还广告飞扬!为了调试,花了我整整一天的时间.给大家节省的时间,也为了让大家少走点弯路. ...
- 泛型算法,排序的相关操作,lower_bound、upper_bound、equal_range
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 用Python的导入csv、文本文件、Excel文件的数据
使用read_csv函数导入CSV文件 read.csv函数语法 read_csv(file,encoding) 例子: Age,Name 22,wangwei 23,lixin 24,liqing ...
- Android : iperf-2.0.4 网络测试工具
一.源码下载及交叉编译: 下载:https://pan.baidu.com/s/1i6NYDF3 //包含linux和windows上的可执行文件 1. 解压后获得perf-2.0.4源码.2. ...
- 单元测试模拟-moq
1.moq 支持 net core 2.moq 通过一个接口类型 可以产生一个新的类 3.举例 //define interface to be mocked public interface ITe ...
- ROS tab键补全操作出现错误
ros tab键补全操作出现错误如下: $ roslaunch sp[rospack] Warning: error while crawling /home/hemudu: boost::files ...
- 杭电多校第七场 1010 Sequence(除法分块+矩阵快速幂)
Sequence Problem Description Let us define a sequence as below f1=A f2=B fn=C*fn-2+D*fn-1+[p/n] Your ...
- 5.Python爬虫入门五之URLError异常处理
大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的 ...
- Asp.Net对Json字符串的解析和应用
using System.Web.Script.Serialization; protected void Page_Load(object sender,EventArgs e) { //构建jso ...