我想为某些URL使用ALLOWED_HOSTS=['*']
,但对于其他URL,我希望它为ALLOWED_HOSTS=[".example.com"]
。
对于csrf,我们有@csrf_exempt
对于cors,我们有信号check_request_enabled
但是要ALLOWED_HOSTS吗?
答案 0 :(得分:1)
一种可行的方法是编写一个中间件,该中间件将检查请求url并设置ALLOWED HOSTS
。您需要在设置文件MIDDLEWARES
部分的顶部中添加此中间件。尝试如下操作:
from django.conf import settings
def simple_middleware(get_response):
def middleware(request):
# Code to be executed for each request before
# the view (and later middleware) are called.
if request.META['PATH_INFO'] == "your_logic":
settings.ALLOWED_HOSTS = ["*"]
else:
settings.ALLOWED_HOSTS = ['example.com']
response = get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
return middleware