Django: Add response header when using render or render_to_response

后端 未结 1 1025
挽巷
挽巷 2021-02-03 20:56

How do I add a response header to a Django response? I have:

response = HttpResponse()
response[\'Cache-Control\'] = \'no-cache\'

return render(request, \"templ         


        
1条回答
  •  梦如初夏
    2021-02-03 21:10

    Assign the result of render to a variable, set the header, then return the response.

    response = render(request, "template.html", {})
    response['Cache-Control'] = 'no-cache'
    return response
    

    Most of the time, it is simpler to user render than render_to_response. However, if you are using render_to_response, the same approach will work:

    response = render_to_response("template.html", {})
    response['Cache-Control'] = 'no-cache'
    return response
    

    0 讨论(0)
提交回复
热议问题