python - Django: Overwrite ROOT_URLCONF with request.urlconf in middleware -
i trying overwrite root_urlconf url when request contains "api" subdomain , have far.
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.
i tried using set_urlconf module "from django.core.urlresolvers" didn't work. missing here?
interestingly, used set_urlconf module , request.urlconf set url path , it's working!
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"
Comments
Post a Comment