How to set cookie in Django and then render template?

后端 未结 4 1899
慢半拍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:21

    This is how to do it:

    from django.shortcuts import render
    
    def home(request, template):
        response = render(request, template)  # django.http.HttpResponse
        response.set_cookie(key='id', value=1)
        return response
    

提交回复
热议问题