gpt4 book ai didi

python - Django:在中间件中用request.urlconf覆盖ROOT_URLCONF

转载 作者:太空狗 更新时间:2023-10-30 02:59:46 24 4
gpt4 key购买 nike

当请求包含“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.

我也尝试使用“来自 django.core.urlresolvers”的 set_urlconf 模块,但它没有用。我在这里遗漏了什么吗?

最佳答案

有趣的是,我使用了 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"

关于python - Django:在中间件中用request.urlconf覆盖ROOT_URLCONF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30928501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com