简体   繁体   English

改变Django的ROOT_URLCONF

[英]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.

相关问题 Django:在中间件中用request.urlconf覆盖ROOT_URLCONF - Django: Overwrite ROOT_URLCONF with request.urlconf in middleware Django:ROOT_URLCONF:如何区分具有两个“urls.py”的同名文件夹? - Django: ROOT_URLCONF: How to distinguish between two folders with same name with two 'urls.py'? 如何在不同分支中为Django ROOT_URLCONF设置设置正确的值 - How to set correct value for Django ROOT_URLCONF setting in different branches 如何解决“ AttributeError:“设置”对象没有属性“ ROOT_URLCONF”” - How to fix “AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'” ImportError:没有名为urls的模块,但ROOT_URLCONF是正确的 - ImportError: no module named urls but ROOT_URLCONF is correct AttributeError:'Settings'对象在Heroku上没有属性'ROOT_URLCONF' - AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF' on Heroku AttributeError: 'Settings' 对象没有属性 'ROOT_URLCONF' - AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF' 无法解决AttributeError:经过大量研究后,“ Settings”对象没有属性“ ROOT_URLCONF”难题 - Cannot solve an AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF' conundrum after doing a lot of research 理解Django的urlconf - Understanding Django's urlconf django urlconf直接从根到包含的应用程序逻辑 - django urlconf direct from root to included app logic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM