一,Django中操作cookie
1,获取cookie
request.COOKIES[=RAISE_ERROR,salt=,max_age=None)
参数:
- default: 默认值
- salt: 加密盐
- max_age: 后台控制过期时间
2,设置cookie
rep =rep.set_cookie(key,value,...)
rep.set_signed_cookie(key,salt=<span style="color: #800000">'<span style="color: #800000">加密盐<span style="color: #800000">',...)
参数:
- 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获取(不是绝对,底层抓包可以获取到也可以被覆盖
3,删除cookie
= redirect()
rep
inner(request,*args,**= request.get_signed_cookie(,salt=,default=None) ==
func(request,**
redirect(<span style="color: #0000ff">def<span style="color: #000000"> login(request):
<span style="color: #0000ff">if request.method == <span style="color: #800000">"<span style="color: #800000">POST<span style="color: #800000">"<span style="color: #000000">:
username = request.POST.get(<span style="color: #800000">"<span style="color: #800000">username<span style="color: #800000">"<span style="color: #000000">)
passwd = request.POST.get(<span style="color: #800000">"<span style="color: #800000">password<span style="color: #800000">"<span style="color: #000000">)
<span style="color: #0000ff">if username == <span style="color: #800000">"<span style="color: #800000">xxx<span style="color: #800000">" <span style="color: #0000ff">and passwd == <span style="color: #800000">"<span style="color: #800000">dashabi<span style="color: #800000">"<span style="color: #000000">:
next_url = request.GET.get(<span style="color: #800000">"<span style="color: #800000">next<span style="color: #800000">"<span style="color: #000000">)
<span style="color: #0000ff">if next_url <span style="color: #0000ff">and next_url != <span style="color: #800000">"<span style="color: #800000">/logout/<span style="color: #800000">"<span style="color: #000000">:
response =<span style="color: #000000"> redirect(next_url)
<span style="color: #0000ff">else<span style="color: #000000">:
response = redirect(<span style="color: #800000">"<span style="color: #800000">/class_list/<span style="color: #800000">"<span style="color: #000000">)
response.set_signed_cookie(<span style="color: #800000">"<span style="color: #800000">login<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">yes<span style="color: #800000">",salt=<span style="color: #800000">"<span style="color: #800000">SSS<span style="color: #800000">"<span style="color: #000000">)
<span style="color: #0000ff">return<span style="color: #000000"> response
<span style="color: #0000ff">return render(request,<span style="color: #800000">"<span style="color: #800000">login.html<span style="color: #800000">"<span style="color: #000000">)
cookie版登录
functools <span style="color: #0000ff">def<span style="color: #000000"> check_login(func):
@wraps(func)
<span style="color: #0000ff">def inner(request,
<span style="color: #000000">kwargs):
next_url =<span style="color: #000000"> request.get_full_path()
<span style="color: #0000ff">if request.session.get(<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">):
<span style="color: #0000ff">return func(request,<span style="color: #000000">kwargs)
<span style="color: #0000ff">else<span style="color: #000000">:
<span style="color: #0000ff">return redirect(<span style="color: #800000">"<span style="color: #800000">/login/?next={}<span style="color: #800000">"<span style="color: #000000">.format(next_url))
<span style="color: #0000ff">return<span style="color: #000000"> inner
<span style="color: #0000ff">def<span style="color: #000000"> login(request):
<span style="color: #0000ff">if request.method == <span style="color: #800000">"<span style="color: #800000">POST<span style="color: #800000">"<span style="color: #000000">:
user = request.POST.get(<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">)
pwd = request.POST.get(<span style="color: #800000">"<span style="color: #800000">pwd<span style="color: #800000">"<span style="color: #000000">)
</span><span style="color: #0000ff">if</span> user == <span style="color: #800000">"</span><span style="color: #800000">alex</span><span style="color: #800000">"</span> <span style="color: #0000ff">and</span> pwd == <span style="color: #800000">"</span><span style="color: #800000">alex1234</span><span style="color: #800000">"</span><span style="color: #000000">:
</span><span style="color: #008000">#</span><span style="color: #008000"> 设置session</span>
request.session[<span style="color: #800000">"</span><span style="color: #800000">user</span><span style="color: #800000">"</span>] =<span style="color: #000000"> user
</span><span style="color: #008000">#</span><span style="color: #008000"> 获取跳到登陆页面之前的URL</span>
next_url = request.GET.get(<span style="color: #800000">"</span><span style="color: #800000">next</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008000">#</span><span style="color: #008000"> 如果有,就跳转回登陆之前的URL</span>
<span style="color: #0000ff">if</span><span style="color: #000000"> next_url:
</span><span style="color: #0000ff">return</span><span style="color: #000000"> redirect(next_url)
</span><span style="color: #008000">#</span><span style="color: #008000"> 否则默认跳转到index页面</span>
<span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #0000ff">return</span> redirect(<span style="color: #800000">"</span><span style="color: #800000">/index/</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #0000ff">return</span> render(request,<span style="color: #800000">"</span><span style="color: #800000">login.html</span><span style="color: #800000">"</span><span style="color: #000000">)
@check_login
<span style="color: #0000ff">def<span style="color: #000000"> logout(request):
<span style="color: #008000">#<span style="color: #008000"> 删除所有当前请求相关的session
<span style="color: #000000"> request.session.delete()
<span style="color: #0000ff">return redirect(<span style="color: #800000">"<span style="color: #800000">/login/<span style="color: #800000">"<span style="color: #000000">)
@check_login
<span style="color: #0000ff">def<span style="color: #000000"> index(request):
current_user = request.session.get(<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">,None)
<span style="color: #0000ff">return render(request,<span style="color: #800000">"<span style="color: #800000">index.html<span style="color: #800000">",{<span style="color: #800000">"<span style="color: #800000">user<span style="color: #800000">"<span style="color: #000000">: current_user})
Session版登录验证
1
=
2<span style="color: #000000">. 缓存Session
SESSION_ENGINE
= <span style="color: #800000">'<span style="color: #800000">django.contrib.sessions.backends.cache<span style="color: #800000">' <span style="color: #008000">#<span style="color: #008000"> 引擎
SESSION_CACHE_ALIAS = <span style="color: #800000">'<span style="color: #800000">default<span style="color: #800000">' <span style="color: #008000">#<span style="color: #008000"> 使用的缓存别名(默认内存缓存,也可以是memcache),此处别名依赖缓存的设置
3<span style="color: #000000">. 文件Session
SESSION_ENGINE = <span style="color: #800000">'<span style="color: #800000">django.contrib.sessions.backends.file<span style="color: #800000">' <span style="color: #008000">#<span style="color: #008000"> 引擎
SESSION_FILE_PATH = None <span style="color: #008000">#<span style="color: #008000"> 缓存文件路径,如果为None,则使用tempfile模块获取一个临时地址tempfile.gettempdir()
- 缓存+<span style="color: #000000">数据库
SESSION_ENGINE = <span style="color: #800000">'<span style="color: #800000">django.contrib.sessions.backends.cached_db<span style="color: #800000">' <span style="color: #008000">#<span style="color: #008000"> 引擎
5<span style="color: #000000">. 加密Cookie Session
SESSION_ENGINE = <span style="color: #800000">'<span style="color: #800000">django.contrib.sessions.backends.signed_cookies<span style="color: #800000">' <span style="color: #008000">#<span style="color: #008000"> 引擎
<span style="color: #000000">
其他公用设置项:
SESSION_COOKIE_NAME = <span style="color: #800000">"<span style="color: #800000">sessionid<span style="color: #800000">" <span style="color: #008000">#<span style="color: #008000"> Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认)
SESSION_COOKIE_PATH = <span style="color: #800000">"<span style="color: #800000">/<span style="color: #800000">" <span style="color: #008000">#<span style="color: #008000"> Session的cookie保存的路径(默认)
SESSION_COOKIE_DOMAIN = None <span style="color: #008000">#<span style="color: #008000"> Session的cookie保存的域名(默认)
SESSION_COOKIE_SECURE = False <span style="color: #008000">#<span style="color: #008000"> 是否Https传输cookie(默认)
SESSION_COOKIE_HTTPONLY = True <span style="color: #008000">#<span style="color: #008000"> 是否Session的cookie只支持http传输(默认)
SESSION_COOKIE_AGE = 1209600 <span style="color: #008000">#<span style="color: #008000"> Session的cookie失效日期(2周)(默认)
SESSION_EXPIRE_AT_BROWSER_CLOSE = False <span style="color: #008000">#<span style="color: #008000"> 是否关闭浏览器使得Session过期(默认)
SESSION_SAVE_EVERY_REQUEST = False <span style="color: #008000">#<span style="color: #008000"> 是否每次请求都保存Session,默认修改之后才保存(默认)