它确实使用一个简单的 dict 来存储 cookie,但是当将 cookie 呈现到响应标头时,django simple 迭代cookies.values()
它不会查看键。
为此,您可以花哨(这是python 3.5):
# python 3.5 specific unpacking
# Note that according to the RFC, cookies ignore the ports
hostname, *_ = request.get_host().split(':')
# set the cookie to delete
response.delete_cookie(settings.ACCESS_TOKEN_COOKIE_KEY,
domain=settings.COOKIE_DOMAIN)
# pull it out of the cookie storage
# and delete it so we can write an new one
cookie_domain_cookie = response.cookies[settings.ACCESS_TOKEN_COOKIE_KEY]
del response.cookies[settings.ACCESS_TOKEN_COOKIE_KEY]
# write the new cookie
response.delete_cookie(settings.ACCESS_TOKEN_COOKIE_KEY,
domain=hostname)
# do the same as we did above, probably not strictly necessary
hostname_cookie = response.cookies[settings.ACCESS_TOKEN_COOKIE_KEY]
del response.cookies[settings.ACCESS_TOKEN_COOKIE_KEY]
# make new keys for the cookies
cookie_domain_cookie_key = "{}:{}".format(settings.ACCESS_TOKEN_COOKIE_KEY, settings.COOKIE_DOMAIN)
hostname_cookie_key = "{}:{}".format(settings.ACCESS_TOKEN_COOKIE_KEY, hostname)
# set them
response.cookies[cookie_domain_cookie_key] = cookie_domain_cookie
response.cookies[hostname_cookie_key] = hostname_cookie