标签:iso tar return logo secure pos url 用户 tool
cookie
request.COOKIES[‘key‘] request.get_signed_cookie(key, default=RAISE_ERROR, salt=‘‘, max_age=None)
参数:
default: 默认值
salt: 加密盐
max_age: 后台控制过期时间
rep = HttpResponse(...) rep = render(request, ...) rep.set_cookie(key,value,...) rep.set_signed_cookie(key,value,salt=‘加密盐‘, max_age=None, ...)
参数:
key, 键
value=‘‘, 值
max_age=None, 超时时间
expires=None, 超时时间(IE requires expires, so set it if hasn‘t been already.)
path=‘/‘, Cookie生效的路径,/ 表示根路径,特殊的:根路径的cookie可以被任何url的页面访问
domain=None, Cookie生效的域名
secure=False, https传输
httponly=False 只能http协议传输,无法被JavaScript获取(不是绝对,底层抓包可以获取到也可以被覆盖)
def logout(request): rep = redirect("/login/") rep.delete_cookie("user") # 删除用户浏览器上之前设置的usercookie值 return rep
Cookie版登陆校验示例:
def check_login(func): @wraps(func) def inner(request, *args, **kwargs): next_url = request.get_full_path() if request.get_signed_cookie("login", salt="SSS", default=None) == "yes": # 已经登录的用户... return func(request, *args, **kwargs) else: # 没有登录的用户,跳转刚到登录页面 return redirect("/login/?next={}".format(next_url)) return inner def login(request): if request.method == "POST": username = request.POST.get("username") passwd = request.POST.get("password") if username == "xxx" and passwd == "dashabi": next_url = request.GET.get("next") if next_url and next_url != "/logout/": response = redirect(next_url) else: response = redirect("/class_list/") response.set_signed_cookie("login", "yes", salt="SSS") return response return render(request, "login.html")
cookie设置中文时的编码问题:cookie在设置时不允许出现中文。非要设置中文的怎么办,看下面的解决方案:
# 方式1 def login(request): ret = HttpResponse(‘ok‘) ret.set_cookie(‘k1‘,‘你好‘.encode(‘utf-8‘).decode(‘iso-8859-1‘)) #取值:request.COOKIES[‘k1‘].encode(‘utf-8‘).decode(‘iso-8859-1‘).encode(‘iso-8859-1‘).decode(‘utf-8‘) return ret 方式2 json def login(request): ret = HttpResponse(‘ok‘) import json ret.set_cookie(‘k1‘,json.dumps(‘你好‘)) #取值 json.loads(request.COOKIES[‘k1‘]) return ret
所以尽量不要出现中文。
标签:iso tar return logo secure pos url 用户 tool
原文地址:https://www.cnblogs.com/yehua-night/p/14639662.html