![](/img/trans.png)
[英]Is modifying Django urlconf via set_urlconf and request.urlconf in a middleware safe?
[英]Django: Overwrite ROOT_URLCONF with request.urlconf in middleware
当请求包含“api”子域时,我试图用另一个URL覆盖ROOT_URLCONF,这是我到目前为止所拥有的。
from django.utils.cache import patch_vary_headers
class SubdomainMiddleware:
def process_request(self, request):
path = request.get_full_path()
root_url = path.split('/')[1]
domain_parts = request.get_host().split('.')
if (len(domain_parts) > 2):
subdomain = domain_parts[0]
if (subdomain.lower() == 'www'):
subdomain = None
else:
subdomain = None
request.subdomain = subdomain
request.domain = domain
if request.subdomain == "api":
request.urlconf = "rest_api_example.urls.api"
else:
request.urlconf = "rest_api_example.urls.
我尝试使用set_urlconf模块“来自django.core.urlresolvers”,但它没有用。 我在这里错过了什么吗?
有趣的是,我使用set_urlconf模块和request.urlconf来设置url路径,现在它正在工作!
from django.core.urlresolvers import set_urlconf
if request.subdomain == "api":
set_urlconf("rest_api_example.urls.api")
request.urlconf = "rest_api_example.urls.api"
else:
set_urlconf("rest_api_example.urls.default")
request.urlconf = "rest_api_example.urls.default"
对于django中的很多东西,已经有了应用程序 - https://github.com/jezdez/django-hosts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.