Django:使用渲染或render_to_response时添加响应标头

问题描述:

如何将响应标头添加到Django响应中?我有:Django:使用渲染或render_to_response时添加响应标头

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

return render(request, "template.html", {}) 

# Alternately using render_to_response 
# return render_to_response("template.html", {}) 

render的结果分配给一个变量,设置标题,然后返回响应。

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

大多数时候,它是简单的用户renderrender_to_response。但是,如果您使用的是render_to_response,则采用相同的方法:

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