I'd like to add a few headers to all responses that my Django website returns. Is there a way to do this (besides adding a wrapper to the render
function)?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes, you should have a look at middlewares.
yourapp/middleware.py
class MyMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
response['X-My-Header'] = "my value"
return response
yourproject/settings.py
MIDDLEWARE = [
...,
'yourapp.middleware.MyMiddleware',
...,
]