简体   繁体   English

Cookie未显示在Chrome开发者工具(Django Python)中

[英]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_visitvisits 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.

相关问题 如何提取显示在 chrome 的开发人员工具上的 href 属性,而不是 BeautifulSoup 的 output - How to extract href attributes that show up on chrome's developer tools, but not on BeautifulSoup's output Django开发人员的工具(和应用程序) - Tools (and applications) for django developer 我可以使用 python 访问谷歌浏览器开发者工具的网络选项卡吗? - can I access to network tab of google chrome developer tools with python? Python 请求标头不起作用 - 检查 Chrome 开发者工具 -> 网络 - Python requests header not working - Checked Chrome developer tools -> Network 如何使用 python 在 chrome 开发人员工具中访问网络选项卡 - How to access network tab in chrome developer tools using python 如何使用 python selenium 访问 google chrome 开发人员工具上的安全面板? - How to access Security panel on google chrome developer tools with python selenium? Chrome开发者工具找不到HttpRequest - Chrome developer tools cannot find HttpRequest chrome开发人员工具显示的HTTP协议不正确? - incorrect http protocol shown by chrome developer tools? 如何使用 python selenium webdriver 在 Chrome 开发人员工具控制台中进行 fetch 调用 - How to make a fetch call in Chrome developer tools console using python selenium webdriver Python:无法使用 Chrome 的开发工具检查浮动 cookie 元素,并且键盘操作正在此元素后面工作 - Python: Floating cookie element can't be inspected with Chrome's dev tools and keyboard actions are working behind this element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM