首页 > 代码库 > Django会话之cookie
Django会话之cookie
In views.py
1 def login(request): 2 context={} 3 if request.method==‘GET‘: 4 return render(request,‘login.html‘,context) 5 else: 6 username=request.POST.get(‘username‘) 7 password=request.POST.get(‘password‘) 8 user = authenticate(username=username, password=password) 9 if user is not None and user.is_active: 10 response=redirect(to=‘contact‘) 11 response.set_cookie("username",username,max_age=3600) 12 return response 13 else: 14 return HttpResponse("Login failed,please go back to try it again")
1 def logout(request):
2 context={}
3 response=redirect(to=‘login‘)
4 # response=HttpResponse(‘You have logouted‘)
5 response.delete_cookie(‘username‘)
6 return response
1 def contact(request):
2 context={}
3 username=request.COOKIES.get("username",‘‘)
4 if username!=‘‘:
5 context[‘username‘]=username
6 return render(request,‘contact.html‘,context)
7 else:
8 return HttpResponse("Please login first")
Django会话之cookie
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。