简体   繁体   中英

Changing Django's ROOT_URLCONF

We are building a multi tenant website, and some tenants want different URLs and such. No big deal, and this is something we already have fixed by making multiple urls.py which can serve as ROOT_URLCONF . When it comes to a request, we simply use our middleware which sets request.urlconf . So far, so good.

But we are also running Celery for a bunch of nightly tasks, and one of those is sending out emails. Here we don't have a request (obviously), so we can't use that to switch the ROOT_URLCONF that way. But changing django.conf.settings.ROOT_URLCONF doesn't work either (which is kinda logical). Is there any way to switch the URLConf that's being used "mid-process"?

It isn't really an option to run multiple Celery instances, each with their own settings because not all objects can be efficiently linked to the tenant they belong to (the views are different, we start with a User which knows it's tenant).

You can use django.core.urlresolvers.set_urlconf :

from django.core.urlresolvers import set_urlconf

set_urlconf('module.path.urls')

That's what django.core.handlers.base.BaseHandler.get_response uses to set up the resolver for the current request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM