在 django 中设置 cookie 时出现问题
我正在尝试使用以下代码设置和读取 cookie,
cookie_name = 'fbs_%s' % practice_settings.PRACTICE_ID
response = HttpResponse( "blah" )
response.set_cookie( cookie_name, "cookie_value" )
value = request.COOKIES.get(cookie_name)
print value
由于某种原因,值仍然为“无”。我在这里缺少一些简单的东西吗?提前致谢
I am trying to set and read cookies using the following code
cookie_name = 'fbs_%s' % practice_settings.PRACTICE_ID
response = HttpResponse( "blah" )
response.set_cookie( cookie_name, "cookie_value" )
value = request.COOKIES.get(cookie_name)
print value
For some reason value remains None. Is there something simple that I am missing here? Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在响应对象中设置 cookie (
response.set_cookie( cookie_name, "cookie_value")
),但尝试从请求对象中检索它 (request.COOKIES.get(cookie_name) )。
当您在响应中设置 cookie 时,它不会自动填充到原始请求中。它将在您设置 cookie 后调用的视图的下一个请求中可用。
You are setting the cookie in the response object (
response.set_cookie( cookie_name, "cookie_value")
), but trying to retrieve it from the request object (request.COOKIES.get(cookie_name)
).When you set a cookie in the response it will not automatically be populated in the original request. It will be available in the following request of the view you call after the one you set the cookie.