Django:使用 render_to_response 并设置 cookie [英] Django: use render_to_response and set cookie

查看:39
本文介绍了Django:使用 render_to_response 并设置 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用 render_to_response(template_name, locals(), context-etc..)

现在正在尝试设置 cookie,我想知道是否可以使用 render_to_response 来实现.

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

我看到的所有示例都使用 HttpResponse 对象.他们在响应对象中设置 cookie,就像这样

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

想知道是否可以使用 render_to_response 设置 cookie,这样我就可以继续使用 locals()

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

谢谢.大卫.

推荐答案

是的,没问题.原理完全一样.

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

这篇关于Django:使用 render_to_response 并设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆