1
我需要創建函數,自上次用戶訪問以來新用戶的計數。我如何使用cookie來做到這一點?自上次用戶訪問後計數新用戶
我嘗試:
觀點:
...
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
)。我應該在我的索引視圖中創建這個函數嗎?什麼是最好的選擇?