Cookie を保存するために単純な dict を使用しますが、Cookie をレスポンス ヘッダーにレンダリングするとき、単純な Djangocookies.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