我正在创建一个cookie来存储客户端中的一些数据(用户访问页面的次数)。在这里,我试图了解客户端cookie。我能够使用服务器端cookie实现所需的结果。但是,在我每次访问页面时设置的客户端cookie中,cookie计数不会增加。下面是我的Django视图中的cookie代码,请帮助我理解我做错了什么。
def index(请求):
if 'visited' in request.COOKIES:
visited = int(request.COOKIES['visited'])
visited += 1
response = render(request,'index.html',context={'visited':visited})
else :
response = render(request,'index.html',context={'visited':0})
response.set_cookie('visited',1)
return response
def index(request):
context = {}
visited = 0
resopnse = render(request,'index.html',{'visited':visited})
if 'visited' in request.COOKIES:
visited = int(request.COOKIES['visited'])
visited += 1
response = render(request,'index.html',{'visited':visited})
response.set_cookie('visited',visited)
return response