如何根据当前请求更改django设置? [英] How to alter django settings based on current request?
问题描述
我正在从一个django实例中运行多个站点,并希望该框架最终可以在一个或多个安装中为数百个站点提供服务。
I'm running multiple sites from a single django instance, and expect the framework to eventually serve several hundred sites from one or several installations.
我需要根据当前请求修补一些django设置。我已经编写了一些中间件来猴子修补设置,但是我需要在调用中间件之前修补这些设置,因为其他应用程序没有进行猴子修补的更改(即先运行应用程序,然后运行中间件,这样应用程序就不会
I need to patch some django settings based on the current request. I've written some middleware to monkey patch the settings, but I need these settings to be patched before the middleware gets invoked because other apps aren't taking the monkey-patched changes (i.e. apps get run then middleware gets run so the apps don't use the monkey-patched settings).
我还应该补充一点,这主要是出于我尚未编写的第三方应用程序的利益,所以我不不想四处添加装饰器之类的东西,因为那样会弄乱我的升级路径。
I should also add this is mainly for the benefit of third-party apps that I haven't written, so I don't want to go round adding decorators or anything like that because that would mess up my upgrade path.
所以:
- 如何在应用程序的 init .py文件中访问当前请求?
- 应用程序的 init .py每个请求被调用一次还是仅被调用一次?如果只有一次,我还可以怎么做,以便每个请求可以操作一次设置?
- 进行这种猴子修补是否安全?我知道这会使代码更加不透明,但是我不想每个站点使用不同的wsgi文件,因为我想允许用户编辑其中的一些设置并使我的猴子补丁来自数据库。
- 是否有更好的解决方案,可以将某些设置存储在数据库中?
- How can I get access to the current request in an app's init.py file?
- Will an app's init.py get called once per request or only once? If it's only once, how else could I do this so I can manipulate the settings once per request?
- Is it safe to do this kind of monkey patching? I know it makes code a bit more opaque, but I don't want to use different wsgi files per site because I want to allow users to edit some of these settings and have my monkey patching come from the database.
- Is there a better solution that would allow certain settings to be stored in the database?
推荐答案
此模块-django-tupperware可以满足您的要求: https:// bitbucket .org / jiaaro / django-tupperware /
试试看。
This module - django-tupperware does what you are asking about: https://bitbucket.org/jiaaro/django-tupperware/ Give it a try.
这篇关于如何根据当前请求更改django设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!