36

如何向 Django 响应添加响应标头?我有:

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

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

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

1 回答 1

54

将结果分配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
于 2013-02-19T11:19:27.283 回答