Django:使用render或render_to_response时添加响应头 [英] Django: Add response header when using render or render_to_response
本文介绍了Django:使用render或render_to_response时添加响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将响应标头添加到Django响应中?我有:
How do I add a response header to a Django response? I have:
response = HttpResponse()
response['Cache-Control'] = 'no-cache'
return render(request, "template.html", {})
# Alternately using render_to_response
# return render_to_response("template.html", {})
推荐答案
分配的结果将 c 渲染为变量,设置标头,然后返回响应。
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
在大多数情况下,用户 render
比 render_to_response $ c $更简单c>。但是,如果您使用
render_to_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
这篇关于Django:使用render或render_to_response时添加响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文