Django利用cookie保存用户登录信息的简单实现方法
本文实例讲述了Django利用cookie保存用户登录信息的方法。分享给大家供大家参考,具体如下:
设置cookie
response对象.set_cookie('key','value',多少秒后过期)获取cookie
request对象.COOKIES.get('key')我们继续前一篇的代码
def hi(request):
msg = {'result':''}
loginSuccess = False # 是否登录成功标识
if user.userLogin(request.POST.get('username'),request.POST.get('pwd')):
loginSuccess = True
msg['result'] = '登录成功'
elif request.COOKIES.get('userlogin_username') != None :
msg['result'] = '已经登录,当前用户是:' + request.COOKIES.get('userlogin_username')
else:
msg['result'] = '登录失败'
myReponse = render_to_response("index.html",msg)
# 如果登录成功,则设置cookies
if loginSuccess:
myReponse.set_cookie('userlogin_username',request.POST.get('username'),3600)
return myReponsehi()方法接收了一个request对象;最后返回一个response对象(该对象由render_to_response函数生成)
希望本文所述对大家基于Django框架的Python程序设计有所帮助。
相关推荐
houmenghu 2020-11-17
kentrl 2020-11-10
jincheng 2020-09-01
Blueberry 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
阳光之吻 2020-08-03
PkJY 2020-07-08
hzyuhz 2020-07-04
89407707 2020-06-27
服务器端攻城师 2020-06-26
阳光岛主 2020-06-25
笨重的蜗牛 2020-06-20
xuanwenchao 2020-06-14
Lophole 2020-06-13
明瞳 2020-06-12
songerxing 2020-06-11