码迷,mamicode.com
首页 > 其他好文 > 详细

基于django开发框架种cookie05-23上

时间:2020-05-29 13:53:24      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:def   开发框架   登录   username   成功   admin   去掉   out   get   

基于代码:

def login(request):
return render(request,"home.html") #给用户一个静态页
@api_view(["POST"])#有页面返回了所以去掉get
def api_login(request):
username = request.POST.get("username")
pwd = request.POST.get("password")
if username is not None and pwd is not None:
if username ==‘admin‘and pwd == "admin":
return render(request,"home.html",context={"username":username})
else:
return render(request, "error.html", context={"msg": "用户名和密码错误"})
else:
return render(request,"error.html",context={"msg":"用户名和密码必填"})
响应回种cookie 请求携带cookie

新内容:响应回种cookie,HttpResponse的方法:
第一个方法:中cookieHttpResponse().set_cookie()#通过响应的cookie回种cookie给请求 这样下次请求就可以带上cookie
需求 登录成功后返回页面 页面携带一个cookie uuid
def login(request):
return render(request,"home.html")
@api_view(["POST"])
def api_login(request):
username = request.POST.get("username")
pwd = request.POST.get("password")
if username is not None and pwd is not None:
if username ==‘admin‘and pwd == "admin":
res = render(request,"home.html",context={"username":username})#返回的url还是login
       res.set_cookie(‘uuid‘,‘1‘)
       return res
else:
return render(request, "error.html", context={"msg": "用户名和密码错误"})
else:
return render(request,"error.html",context={"msg":"用户名和密码必填"})
验证效果:127.0.0.1:8000/user/api/login  检查下application-cookies

需求二:登录成功后 实现返回首页 用重定向到首页 引入HttpResponseRedirect
from django.http import HttpResponse,JsonResponse,HttpResponseRedirect(状态码)
user/urls文件增加home.htm页面
from django.urls import path
from users import views
urlpatterns = [
path(‘hello/‘, views.hello),
path(‘login/‘,views.login),
path(‘home/‘,views.home),#增加的页面
path(‘api/login/‘,views.api_login),
path(‘api/logout/‘,views.api_logout)]
user/views文件增加home请求
def home1(request):
  return render(request,‘home.html‘)
@api_view(["POST"])
def api_login(request):
username = request.POST.get("username")
pwd = request.POST.get("password")
if username is not None and pwd is not None:
if username ==‘admin‘and pwd == "admin":
       res = HttpResponseRedirect(‘/user/home’)#重定向到首页
       res.set_cookid(‘uid‘,‘‘1)#种cookie
       return res
        else:
return render(request, "error.html", context={"msg": "用户名和密码错误"})
else:
return render(request,"error.html",context={"msg":"用户名和密码必填"})
需求三:返回home首页的时候,home接口需要判断登录状态 cookie是否存在;有就在首页 没有就回到登录 未设置cookie失效实际 默认关闭浏览器cookie失效
def home1(request):
  if request.COOKIES.get(‘uid‘) ==‘1‘:
    
    return render(request,‘home.html‘)
  else:
    return HttpResponseRedirect(‘/user/login’)
@api_view(["POST"])
def api_login(request):
username = request.POST.get("username")
pwd = request.POST.get("password")
if username is not None and pwd is not None:
if username ==‘admin‘and pwd == "admin":
       res = HttpResponseRedirect(‘/user/home’)#重定向到首页
       res.set_cookid(‘uid‘,‘‘1)#种cookie
       return res
        else:
return render(request, "error.html", context={"msg": "用户名和密码错误"})
else:
return render(request,"error.html",context={"msg":"用户名和密码必填"})
需求三:上面cookies没加密



基于django开发框架种cookie05-23上

标签:def   开发框架   登录   username   成功   admin   去掉   out   get   

原文地址:https://www.cnblogs.com/qd1228/p/12986874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!