开发者

Problem setting cookies in django

开发者 https://www.devze.com 2023-02-26 23:53 出处:网络
I am trying to set and read cookies using the following code cookie_name = \'fbs_%s\' % practice_settings.PRACT开发者_JAVA百科ICE_ID

I am trying to set and read cookies using the following code

cookie_name = 'fbs_%s' % practice_settings.PRACT开发者_JAVA百科ICE_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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号