感谢武沛齐老师 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整理的更多相关文章

  1. Django笔记-常见错误整理

    1.csrf错误 解决方法:在settings.py里注释掉相关内容即可 MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.Sess ...

  2. Django框架 之 Cookie、Session整理补充

    Django框架 之 Cookie.Session整理补充 浏览目录 Django实现的Cookie Django实现的Session 一.Django实现的Cookie 1.获取Cookie 1 2 ...

  3. Django学习笔记之Cookie、Session和自定义分页

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  4. SQL反模式学习笔记10 取整错误

    目标:使用小数取代整数 反模式:使用Float类型 根据IEEE754标识,float类型使用二进制格式编码实数数据. 缺点:(1)舍入的必要性: 并不是所有的十进制中描述的信息都能使用二进制存储,处 ...

  5. django中操作cookie与session

    cookie 什么是Cookie Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务器提取有用信息. Cookie的 ...

  6. django(五):cookie和session

    一.Cookie 1.cookie机制 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确 ...

  7. Python学习---django下的cookie操作 180201

    什么是Cookies 什么是Cookies cookies设置的原因: 1. http请求的无记忆性: 2.加快访问速度  3. 减少服务器压力 cookies特点: cookies保存在客户端浏览器 ...

  8. 【转】Django中的cookie与session

    转自:https://www.cnblogs.com/chenchao1990/p/5283725.html cookie与session的实现原理 HTTP被设计为”无状态”,每次请求都处于相同的空 ...

  9. 【python】-- Django 分页 、cookie、Session、CSRF

    Django  分页 .cookie.Session.CSRF 一.分页 分页功能在每个网站都是必要的,下面主要介绍两种分页方式: 1.Django内置分页 from django.shortcuts ...

随机推荐

  1. HDU 4035

    dp求期望的题. 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望.E[1]即为所求. 叶子结点: E[i] = ki*E[1] + ei*0 + (1-ki-ei)*(E[father[i] ...

  2. hello world to php( mac 配置 xmapp virtual host)

    一.安装xmapp.安装完以后查看,服务是否都能启动(数据库和server) 二.配置自己的virtualhost       1.系统host文件加入server的域名(在浏览器中输入域名后会先通过 ...

  3. 从100PV到1亿级PV站点架构演变

    假设你对项目管理.系统架构有兴趣,请加微信订阅号"softjg".增加这个PM.架构师的大家庭 一个站点就像一个人,存在一个从小到大的过程. 养一个站点和养一个人一样.不同一时候期 ...

  4. video_capture模块分析

    1. 对外接口      VideoCaptureModule     控制接口      VideoCaptureDataCallback Vie中的ViECapturer继承,用于响应抓包数据 2 ...

  5. Codeforces 10A-Power Consumption Calculation(模拟)

    A. Power Consumption Calculation time limit per test 1 second memory limit per test 256 megabytes in ...

  6. Popupwindow 显示, 其它背景变暗。 并加上点击事件 ~ (用于记录)

    public class MainActivity extends Activity implements OnClickListener { protected int mScreenWidth; ...

  7. JAVA设计模式之【外观模式】

    通过引入一个外观角色来简化客户端与子系统之间的交互. 顾客无需直接和茶叶.茶具.开水等交互,整个泡茶过程由服务员来完成,顾客只需与服务员交互即可. 通过引入一个外观角色可以降低原有系统的复杂度,同时降 ...

  8. CoreData 从入门到精通(二) 数据的增删改查

    在上篇博客中,讲了数据模型和 CoreData 栈的创建,那下一步就是对数据的操作了.和数据库一样,CoreData 里的操作也无非是增删改查.下面我们将逐步讲解在 CoreData 中进行增删改查的 ...

  9. python 提取主域名和子域名代码——先根据规则提取,如果有问题,则使用tldextract

    import tldextract def extract_domain(domain): suffix = {'.com','.la','.io', '.co', '.cn','.info', '. ...

  10. Ext4,Ext3的特点和区别

    Linux kernel 自 2.6.28 开始正式支持新的文件系统 Ext4. Ext4 是 Ext3 的改进版,修改了 Ext3 中部分重要的数据结构,而不仅仅像 Ext3 对 Ext2 那样,只 ...