我需要创建自上次用户访问以来新用户数量的函数。我怎么能用cookies做到这一点?
我的尝试:
的观点:
...
users = User.objects.all().count()
response = HttpResponse()
if not request.COOKIES.get('new_users'):
response.set_cookie('new_users', 0)
n = request.COOKIES.get('new_users')
else:
old = request.COOKIES.get('new_users')
new = int(users) - int(old)
if new >=1:
response.set_cookie('new_users', new)
n = request.COOKIES.get('new_users')
return render_to_response('main.html', {'n': n}, context_instance=RequestContext(request))
但仍有一些奇怪的值(在n
中)。我应该在索引视图中创建此函数还是在哪里?什么是最好的选择?