Django: Add response header when using render or render_to_response

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)