【问题标题】:Django: use render_to_response and set cookieDjango:使用 render_to_response 并设置 cookie
【发布时间】:2011-06-26 06:42:53
【问题描述】:

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

现在正在尝试设置一个 cookie,我想知道是否可以使用 render_to_response 进行设置。

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

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

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

谢谢。 大卫。

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

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

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

    【讨论】:

    • 这将呈现页面并设置 cookie,因此对于第一次运行,“favorite_color”可能不存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 2021-06-22
    相关资源
    最近更新 更多