How to set cookie in Django and then render template?

后端 未结 4 1877
慢半拍i
慢半拍i 2020-12-25 14:07

I want to set a cookie inside a view and then have that view render a template. As I understand it, this is the way to set a cookie:

def index(request):
            


        
4条回答
  •  隐瞒了意图╮
    2020-12-25 14:19

    The accepted answer sets the cookie before the template is rendered. This works.

    response = HttpResponse()
    response.set_cookie("cookie_name", "cookie_value")
    response.write(template.render(context))
    

提交回复
热议问题