django笔记10 cookie整理
感谢武沛齐老师 Alex老师
cookie 没有cookie所有的网站都登录不上
客户端浏览器上的一个文件
{'user':'ljc'}
{"user":'zpt'}
request.COOKIES.get('..')
response.set_cookie('..') 加密
obj = HttpResponse('s')
obj.set_signed_cookie('username',"kangbazi",salt="afajkfa") 解密
var w = request.get_signed_cookie('username',salt="afajkfa") import hashlib
m = hashlib.md5('fdaf')
m.update('afda') 装饰器: def auth(func): #装饰器
def inner(request,*args, **kwargs):
v = request.COOKIES.get('username111')
if not v:
return redirect('/login/')
return func(request,*args, **kwargs)
return inner FBV:
def auth(func):
def inner(request,*args, **kwargs):
v = request.COOKIES.get('username111')
if not v:
return redirect('/login/')
return func(request,*args, **kwargs)
return inner CBV:
from django import views
from django.utils.decorators import method_decorator @method_decorator(auth, name='dispatch')
class Order(views.View):
# @method_decorator(auth)
# def dispatch(self, request, *args, **kwargs):
# return super(Order,self).dispatch(request,*args, **kwargs) @method_decorator(auth)
def get(self,request):
v = request.COOKIES.get('username111')
return render(request, 'index.html', {'current_user': v}) def post(self,request):
v = request.COOKIES.get('username111')
return render(request, 'index.html', {'current_user': v})
cookie1:
request.COOKIES.get('username111') #获取用户端发来的cookie 获取 response = render(request,"index.html")
response = redirect('/index/')
#设置cookie 关闭浏览器失效(还可以设置超时时间)
response.set_cookie('key',"value") #关闭浏览器时 才失效 设置
return response cookie2:
request.COOKIES.get('username111') #获取用户端发来的cookie response = render(request,"index.html")
response = redirect('/index/')
#设置cookie 10秒后会过期 max_age = 截止时间失效
response.set_cookie('key',"value",max_age = )
import datetime
current_time = datetime.datetime.utcnow()
current_time = current_date + datetime.timedelta(seconds=)
#expires 具体到哪个时间节点
response.set_cookie('key',"value", expires = current_time)
return response cookie3: #document.cookie
request.COOKIES.get('username111') #获取用户端发来的cookie response = render(request,"index.html")
response = redirect('/index/')
#设置cookie 关闭浏览器失效(还可以设置超时时间)
#path='/'默认的地址
#domain=None 生效的域名
#secure=False https传输 如果是 要设置为true
#httponly=True 加上他好一点 在js获取cookie时 获取不到
response.set_cookie('key',"value") #关闭浏览器时 才失效
return response user_info = {
'dachengzi':{'pwd':''},
'kanbazi':{'pwd':''},
}
def login(request):
if request.method =="GET":
return render(request,'login.html')
if request.method == "POST":
u = request.POST.get('username')
p = request.POST.get('pwd')
dic = user_info.get(u)
if not dic:
return render(request, 'login.html')
if dic['pwd'] == p:
res = redirect('/index/')
res.set_cookie('username111',u)
return res
else:
return render(request, 'login.html') def index(request):
#获取当前已经登录的用户名
v = request.COOKIES.get('username111')
if not v:
return redirect('/login') return render(request,'index.html',{'current_user':v}) 缓存
中间件
信号
CSRF
Admin
ModelForm
django笔记10 cookie整理的更多相关文章
- Django笔记-常见错误整理
1.csrf错误 解决方法:在settings.py里注释掉相关内容即可 MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.Sess ...
- Django框架 之 Cookie、Session整理补充
Django框架 之 Cookie.Session整理补充 浏览目录 Django实现的Cookie Django实现的Session 一.Django实现的Cookie 1.获取Cookie 1 2 ...
- Django学习笔记之Cookie、Session和自定义分页
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- SQL反模式学习笔记10 取整错误
目标:使用小数取代整数 反模式:使用Float类型 根据IEEE754标识,float类型使用二进制格式编码实数数据. 缺点:(1)舍入的必要性: 并不是所有的十进制中描述的信息都能使用二进制存储,处 ...
- django中操作cookie与session
cookie 什么是Cookie Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务器提取有用信息. Cookie的 ...
- django(五):cookie和session
一.Cookie 1.cookie机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确 ...
- Python学习---django下的cookie操作 180201
什么是Cookies 什么是Cookies cookies设置的原因: 1. http请求的无记忆性: 2.加快访问速度 3. 减少服务器压力 cookies特点: cookies保存在客户端浏览器 ...
- 【转】Django中的cookie与session
转自:https://www.cnblogs.com/chenchao1990/p/5283725.html cookie与session的实现原理 HTTP被设计为”无状态”,每次请求都处于相同的空 ...
- 【python】-- Django 分页 、cookie、Session、CSRF
Django 分页 .cookie.Session.CSRF 一.分页 分页功能在每个网站都是必要的,下面主要介绍两种分页方式: 1.Django内置分页 from django.shortcuts ...
随机推荐
- PHP第八课 字符串拆分经常使用函数
课程概要: 通过这节课可以对字符串进行主要的操作. 字符串知识点: 1.字符串的处理介绍 2.经常使用的字符串输出函数 3.经常使用的字符串格式化函数 4.字符串比較函数 5.正則表達式在字符串中的应 ...
- php利用msqli访问数据库并实现分页,
<?php require_once 'login.php'; $num_rec_per_page=2; // 每页显示数量 //mysql_connect('localhost','jim', ...
- linux 内存不足时候 应该及时回收page cache
另一起问题是24G内存的系统,空闲内存已经不到50M 1. 确认该系统的版本是64位 # uname -a Linux gxgd-nms-app 2.6.18-194.el5xen #1 SMP Tu ...
- windows模式编译
//预编译,linker链接,Windows模式#pragma comment(linker,"/subsystem:\"windows\" /entry:\" ...
- 转:利用 T-sql 的从句 for xml path('') 实现多行合并到一行, 并带有分隔符
http://blog.csdn.net/rav009/article/details/50723307 T-sql 有一个for xml path('')的从句能把多行结果合并到一行,并成为xml ...
- rsyslog 存储到 mysql
输出Host1/2的系统日志, 记录到mysql服务器数据库中, 并发布loganalyzer 结构关系如下图: 思路: 通过远程连接mysql, 使得rsyslog的log记录能够写入到mysql中 ...
- java 实现多线程 3种方式
java实现多线程可以有以下三种方式: (1)继承Thread 类,重写其run()方法: (2)实现Runnable接口,实现其run() 方法: (3) 实现Callable 接口,重写call( ...
- 搭建javawebxiangmu
https://blog.csdn.net/qq_23994787/article/details/73612870#
- (2016北京集训十)【xsy1530】小Q与内存
一道很有意思的神题~ 暴力平衡树的复杂度很对(并不),但是$2^{30}$的空间一脸屎 这题的正解是一个类似线段树的数据结构,我觉得很有创新性Orz 首先可以想到一种暴力就是用一个点代表一个区间,然后 ...
- LightOJ-1336 Sigma Function 唯一分解定理 巧妙使用sqrt()等算数目
题目链接:https://cn.vjudge.net/problem/LightOJ-1336 题意 给出一个区间[1, n],求区间内所有数中因数之和为偶数的数目 思路 第二次写这个题 首先想到唯一 ...