如何显示django-cookie的到期时间? [英] How to display the expiration time of django-cookie?

查看:471
本文介绍了如何显示django-cookie的到期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 getCookie 函数中,我想显示名称和给定的cookie的过期时间,但我能够归档的是名称

In my getCookie function, I'd like to display the name AND the expiration-time of given cookie, but all I've been able to archive is the name of it, as a string.

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

The cookie is set, and expires as expected, I'd just like to show the remaining time of it, in a simple way.

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)


推荐答案

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

request.COOKIES only contains the key/value mapping of the parsed cookies. However request.META['HTTP_COOKIE'] contains the raw cookie string which can be parsed by the Python cookie.SimpleCookie class to get the max-age http://docs.python.org/library/cookie.html

这篇关于如何显示django-cookie的到期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆