0

在我的getCookie函数中,我想显示给定 cookie 的名称和过期时间,但我能够归档的只是它的名称,作为一个字符串。

cookie 已设置,并按预期过期,我只想以简单的方式显示它的剩余时间。

def setCookie(request):
    cook = HttpResponseRedirect('/getCookie/')
    cook.set_cookie('theCookie', value='Dough', max_age=15)

    return cook

def getCookie(request):
    s=""
    if request.COOKIES.has_key('theCookie'):
        s += request.COOKIES['theCookie']
        #s += request.COOKIES['theCookie'].expires..? HERE?

    else:
        s="Sorry, your cookie has expired"
    return HttpResponse(s)
4

4 回答 4

5

request.COOKIES仅包含已解析 cookie 的键/值映射。但是request.META['HTTP_COOKIE']包含可以由 Python cookie.SimpleCookie类解析以获取max-age http://docs.python.org/library/cookie.html的原始 cookie 字符串

于 2012-10-10T16:09:02.100 回答
4

cookie 的过期时间可以这样计算:

import Cookie
val = request.META['HTTP_COOKIE']
c = Cookie.SimpleCookie()
c.load(val)
c['cookie-name'].keys()
... 
comment,domain,secure,expires,max-age,version,path,httponly

expires = c['cookie-name']['expires']
于 2018-04-16T09:28:06.417 回答
-5

简短的回答:它不能完成。

于 2012-10-10T15:55:49.013 回答
-6

尝试get_expiry_age:

request.COOKIES['theCookie'].get_expiry_age()
于 2012-10-10T11:19:23.883 回答