[英]Cookie does not show up in Chrome Developer Tools (Django Python)
Some background: I've been working on the tangowithdjango.com tutorial for the past few weeks and am stuck on how to get cookies to show up on the "Developer Tools" widget in the Chrome browser. 背景知识:在过去的几周里,我一直在致力于tangowithdjango.com教程,并且一直在研究如何使Cookie出现在Chrome浏览器的“开发人员工具”小部件中。 I'm running Python 2.7.5 and Django 1.5.4 on a Mac OS X Mountain Lion.
我在Mac OS X Mountain Lion上运行Python 2.7.5和Django 1.5.4。
Now to the problem at hand: I'm building a webpage using Django as part of the tutorial. 现在要解决的问题是:作为教程的一部分,我正在使用Django构建网页。 The current exercise I'm stuck on requires me to do some work with cookies.
我目前坚持的练习要求我做一些cookie工作。 I used the code given to me in the tutorial to set up a site visit counter that increments once a day when a user visits my site.
我使用了教程中提供的代码来设置站点访问计数器,当用户访问我的站点时,该计数器每天增加一次。 Here is the code I have in the
views.py
file for my index.html page (Home page): 这是我在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-----
NOTE: please start reading from the comment "IMPORTANT CODE STARTS HERE" 注意:请从注释“重要代码在这里开始”开始阅读
What I should be seeing is as follows: 我应该看到的如下:
Notice how the last_visit
and visits
cookies show up. 请注意
last_visit
和visits
Cookie的显示方式。 These are the ones I'm not seeing on my Developer Tools once I run the code. 这些是我运行代码后在开发人员工具上看不到的内容。 The picture below illustrates what I see on my web browser:
下图说明了我在网络浏览器中看到的内容:
Can someone please explain to me why I'm not able to see these two cookies even after my code explicitly set them in views.py
? 有人可以向我解释为什么即使我的代码在
views.py
中将它们显式设置后,我仍然看不到这两个cookie吗?
You check if the last_visit cookie already exists, and only update it if it does. 您检查last_visit cookie是否已经存在,并且仅在存在时更新它。 What if it doesn't?
如果没有,该怎么办? Where are you creating it for the first time?
您是第一次在哪里创建它?
I suspect an indentation error: the last else
block should be one level to the left, so it is run if last_visit does not exist, as the comment states. 我怀疑出现缩进错误:最后的
else
块应该在左侧一级,因此如注释所述,如果last_visit不存在,则运行该块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.