简体   繁体   中英

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. I'm running Python 2.7.5 and Django 1.5.4 on a Mac OS X Mountain Lion.

Now to the problem at hand: I'm building a webpage using Django as part of the tutorial. The current exercise I'm stuck on requires me to do some work with cookies. 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):

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. 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 ?

You check if the last_visit cookie already exists, and only update it if it does. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM