文章目录
- 参考资料
- 会话保持
- Cookies
- views.py
- urls.py
- Session
- views.py
- urls.py
- Cookies和session对比
参考资料
B站网课:点击蓝色字体跳转
或复制链接至浏览器:https://www.bilibili.com/video/BV1vK4y1o7jH/?p=29&spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=597e21cf34ffcdce468ba00be2177e8a
会话保持
Http的请求与相应对应着家和商店之间的交易,家相当于浏览器,商店相当于服务器,通过办会员卡的方式可以保存交易记录。
Cookies
views.py
def set_cookies(request):resp = HttpResponse('set cookies is ok')resp.set_cookie('uuname','syw',500)return respdef get_cookies(request):value = request.COOKIES.get('uuname')return HttpResponse('value is %s'%(value))
urls.py
path('set_cookies',views.set_cookies),path('get_cookies',views.get_cookies),
Session
views.py
def set_session(request):request.session['uname']='czn'return HttpResponse('set session is ok')def get_session(request):value=request.session['uname']return HttpResponse('session value is %s'%(value))
urls.py
path('set_session',views.set_session),path('get_session',views.get_session),