Cookie 未显示在 Chrome 开发人员工具中 (Django Python)
Cookie does not show up in Chrome Developer Tools (Django Python)
一些背景知识: 过去几周我一直在研究 tangowithdjango.com 教程,一直在研究如何让 cookie 显示在 "Developer Tools" 小部件在 Chrome 浏览器中。我在 运行ning Python 2.7.5 和 Django 1.5.4 Mac OS X Mountain Lion.
现在解决手头的问题: 作为本教程的一部分,我正在使用 Django 构建一个网页。我目前坚持的练习要求我用 cookie 做一些工作。我使用教程中提供的代码设置了一个站点访问计数器,该计数器每天在用户访问我的站点时递增一次。这是我的 index.html 页面(主页)的 views.py
文件中的代码:
def index(request):
context = RequestContext(request)
category_list = Category.objects.all()
top_five_cats = Category.objects.order_by('-views')[:5]
if enc_bool == False:
EncodeUrl(category_list, top_five_cats)
context_dict = {'categories': category_list, 'top_five_cats': top_five_cats}
# Obtain our Response object early so we can add cookie information.
response = render_to_response('rango/index.html', context_dict, context)
#-----IMPORTANT CODE STARTS HERE:-----
# Get the number of visits to the site.
# We use the COOKIES.get() function to obtain the visits cookie.
# If the cookie exists, the value returned is casted to an integer.
# If the cookie doesn't exist, we default to zero and cast that.
visits = int(request.COOKIES.get('visits', '0'))
print "visits: ",visits
# Does the cookie last_visit exist?
if 'last_visit' in request.COOKIES:
# Yes it does! Get the cookie's value.
last_visit = request.COOKIES['last_visit']
print "last visit: ", last_visit
print
# Cast the value to a Python date/time object.
last_visit_time = datetime.strptime(last_visit[:-7], "%Y-%m-%d %H:%M:%S")
# If it's been more than a day since the last visit...
if (datetime.now() - last_visit_time).days > 0:
# ...reassign the value of the cookie to +1 of what it was before...
response.set_cookie('visits',visits+1)
# ...and update the last visit cookie, too.
response.set_cookie('last_visit', datetime.now())
else:
# Cookie last_visit doesn't exist, so create it to the current date/time.
response.set_cookie('last_visit', datetime.now())
return response
#-----IMPORTANT CODE ENDS-----
注意:请从评论开始阅读"IMPORTANT CODE STARTS HERE"
我应该看到的如下:
注意 last_visit
和 visits
cookie 是如何显示的。这些是我在 运行 代码后在我的开发人员工具上看不到的。下图说明了我在网络浏览器上看到的内容:
有人可以向我解释为什么即使我的代码在 views.py
中明确设置了这两个 cookie,我还是看不到它们吗?
您检查 last_visit cookie 是否已经存在,如果存在则只更新它。如果没有呢?您是在哪里第一次创建它?
我怀疑是缩进错误:最后一个 else
块应该在左边一层,所以如果 last_visit 不存在,它就是 运行,如评论所述。
一些背景知识: 过去几周我一直在研究 tangowithdjango.com 教程,一直在研究如何让 cookie 显示在 "Developer Tools" 小部件在 Chrome 浏览器中。我在 运行ning Python 2.7.5 和 Django 1.5.4 Mac OS X Mountain Lion.
现在解决手头的问题: 作为本教程的一部分,我正在使用 Django 构建一个网页。我目前坚持的练习要求我用 cookie 做一些工作。我使用教程中提供的代码设置了一个站点访问计数器,该计数器每天在用户访问我的站点时递增一次。这是我的 index.html 页面(主页)的 views.py
文件中的代码:
def index(request):
context = RequestContext(request)
category_list = Category.objects.all()
top_five_cats = Category.objects.order_by('-views')[:5]
if enc_bool == False:
EncodeUrl(category_list, top_five_cats)
context_dict = {'categories': category_list, 'top_five_cats': top_five_cats}
# Obtain our Response object early so we can add cookie information.
response = render_to_response('rango/index.html', context_dict, context)
#-----IMPORTANT CODE STARTS HERE:-----
# Get the number of visits to the site.
# We use the COOKIES.get() function to obtain the visits cookie.
# If the cookie exists, the value returned is casted to an integer.
# If the cookie doesn't exist, we default to zero and cast that.
visits = int(request.COOKIES.get('visits', '0'))
print "visits: ",visits
# Does the cookie last_visit exist?
if 'last_visit' in request.COOKIES:
# Yes it does! Get the cookie's value.
last_visit = request.COOKIES['last_visit']
print "last visit: ", last_visit
print
# Cast the value to a Python date/time object.
last_visit_time = datetime.strptime(last_visit[:-7], "%Y-%m-%d %H:%M:%S")
# If it's been more than a day since the last visit...
if (datetime.now() - last_visit_time).days > 0:
# ...reassign the value of the cookie to +1 of what it was before...
response.set_cookie('visits',visits+1)
# ...and update the last visit cookie, too.
response.set_cookie('last_visit', datetime.now())
else:
# Cookie last_visit doesn't exist, so create it to the current date/time.
response.set_cookie('last_visit', datetime.now())
return response
#-----IMPORTANT CODE ENDS-----
注意:请从评论开始阅读"IMPORTANT CODE STARTS HERE"
我应该看到的如下:
注意 last_visit
和 visits
cookie 是如何显示的。这些是我在 运行 代码后在我的开发人员工具上看不到的。下图说明了我在网络浏览器上看到的内容:
有人可以向我解释为什么即使我的代码在 views.py
中明确设置了这两个 cookie,我还是看不到它们吗?
您检查 last_visit cookie 是否已经存在,如果存在则只更新它。如果没有呢?您是在哪里第一次创建它?
我怀疑是缩进错误:最后一个 else
块应该在左边一层,所以如果 last_visit 不存在,它就是 运行,如评论所述。