# 路由
urlpatterns = [
path("setcookie/", views.setCookie),
path("delcookie/", views.delCookie),
]
# 视图代码
from django.http import HttpResponse
# Create your views here.
def setCookie(request):
if request.COOKIES.get('status') is None:
# 获取不到cookie,说明没有登录
resp = HttpResponse("第一次登录,生成cookie")
resp.set_cookie('status', 'login') # 设置cookie
return resp # 返回响应
else:
cookieData = request.COOKIES.get('status')
return HttpResponse(f"当前的状态为:{cookieData}")
def delCookie(request):
if request.COOKIES.get('status'):
# 能够获取到cookie,进行删除
resp = HttpResponse("删除cookie成功")
resp.delete_cookie("status") # 删除cookie
return resp # 返回响应
else:
return HttpResponse("cookie不存在")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)