[英]Changing Django's ROOT_URLCONF
We are building a multi tenant website, and some tenants want different URLs and such. 我们正在建立一个多租户网站,一些租户需要不同的URL等。 No big deal, and this is something we already have fixed by making multiple urls.py which can serve as
ROOT_URLCONF
. 没什么大不了的,这是我们已经通过制作多个urls.py来解决的问题,它可以作为
ROOT_URLCONF
。 When it comes to a request, we simply use our middleware which sets request.urlconf
. 当涉及到请求时,我们只需使用设置
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. 但我们也在为一堆夜间任务运行Celery,其中一个就是发送电子邮件。 Here we don't have a
request
(obviously), so we can't use that to switch the ROOT_URLCONF
that way. 在这里我们没有(显然)
request
,所以我们不能用那个方式切换ROOT_URLCONF
。 But changing django.conf.settings.ROOT_URLCONF
doesn't work either (which is kinda logical). 但是改变
django.conf.settings.ROOT_URLCONF
也不起作用(这有点合乎逻辑)。 Is there any way to switch the URLConf that's being used "mid-process"? 有没有办法切换正在使用的“中间过程”的URLConf?
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). 运行多个Celery实例并不是一个真正的选项,每个实例都有自己的设置,因为并非所有对象都可以有效地链接到它们所属的租户(视图不同,我们从知道它的租户的
User
开始)。
You can use django.core.urlresolvers.set_urlconf
: 你可以使用
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. 这就是
django.core.handlers.base.BaseHandler.get_response
用于为当前请求设置解析器的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.