明瞳 2020-06-12
from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
class Order(APIView):
def get(self, request, *args, **kwargs):
response= render(request,‘login.html‘)
response.set_cookie(‘username‘,‘xiaoming‘) # 设置cookie
return response
def post(self, request, *args, **kwargs):
ret = request.COOKIES[‘username‘] # 获取cookie
return HttpResponse(ret)from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
class Order(APIView):
def GET(self, request, *args, **kwargs):
response = HttpResponse("ok")
response.set_cookie("username","xiaoming") # 设置cookie
return response
def GET(self, request, *args, **kwargs):
ret = request.COOKIES["username"] # 获取cookie
res = request.COOKIES.get("username") # 获取cookie
return Httpresponse("success")request.COOKIES[‘key‘] request.get_signed_cookie(key, default=RAISE_ERROR, salt=‘‘, max_age=None)
参数:
rep = HttpResponse(...) rep = render(request, ...) rep.set_cookie(key,value,...) rep.set_signed_cookie(key,value,salt=‘加密盐‘,...)
参数:
def logout(request):
rep = redirect("/login/")
rep.delete_cookie("user") # 删除用户浏览器上之前设置的usercookie值
return rep结束!