首页 > 代码库 > Linux下开发python django程序(Cookie读写)
Linux下开发python django程序(Cookie读写)
1.设置cookie信息(登陆成功后设置登陆用户名,有效期1小时)
def login(req): if req.method == ‘POST‘: loginform = LoginForm(req.POST) if loginform.is_valid(): print loginform.cleaned_data username = loginform.cleaned_data["username"] password = loginform.cleaned_data["userpwd"] users=RegisterUser.objects.filter(username =username , password =password) print users if users: response= HttpResponseRedirect(‘/app1/index1/‘) response.set_cookie(‘username‘,username,3600) return response else: return HttpResponseRedirect(‘/app1/login/‘) else: loginform = LoginForm() return render_to_response(‘login.html‘,{‘loginform‘:loginform})
2.检查获取当前cookie信息(判断是否登陆)
def index1(req): loginusername = req.COOKIES.get(‘username‘,‘‘) print loginusername islogin=False if loginusername: islogin=True else: islogin=False aus = Author.objects.all() books = Book.objects.all() t= loader.get_template(‘index1.html‘) c=Context({‘aus‘:aus,‘books‘:books,‘islogin‘:islogin,‘loginusername‘:loginusername}) return HttpResponse(t.render(c))
3.删除cookie信息
def loginout(req): response =HttpResponseRedirect(‘/app1/index1/‘) response.delete_cookie(‘username‘) return response
Linux下开发python django程序(Cookie读写)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。