Django: use render_to_response and set cookie

百般思念 提交于 2019-12-30 04:15:12

问题


Currently, I'm using render_to_response(template_name, locals(), context-etc..)

Trying to set a cookie right now and I'm wondering if I can do it with render_to_response.

All the examples I see are using HttpResponse object. They set the cookie in the response object, like this

response = HttpResponseObject(html)
response.set_cookie("favorite_color",request.GET["favorite_color"])
return response

Wondering if I can set cookie with render_to_response, so I can continue using locals()

Thank you. David.


回答1:


Yes, not a problem. The principle is exactly the same.

response = render_to_response(template_name, locals(), context-etc..)
response.set_cookie("favorite_color",request.GET["favorite_color"])
return response


来源:https://stackoverflow.com/questions/4981601/django-use-render-to-response-and-set-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!