Django 动态设置基础架构和最佳实践
Django 设置包含一个 Python 变量列表,这些变量用于从数据库设置到已安装的应用程序等多种操作。甚至许多可重复使用的应用程序也需要进行一些设置。
由于有数十个站点,因此很难管理所有项目的设置。
幸运的是,settings 只是一个带有变量的 python 模块,所以你可以使用任何魔法来填充你想要的变量。
您遵循或认为可以使用哪些实践将各种相关设置分离到不同的文件中?
显然,现有的企业实践是开发人员制造一场战争,然后运维部门将其打给蓝鱼并负责所有数据库(以及此类)运维内容(根据 Jacob 的电子邮件)。
您可以创建哪些动态settings.py
来帮助现有的企业实践?
Django settings includes a list of python variables that are used for a plethora of things from database settings to installed apps. Even many of the reusable apps make some of the settings required.
With a dozens of sites, it is hard to manage the settings of all the projects.
Fortunately settings is just a python module with variables, so you can do any magic to populate the variables you want.
What practices have you followed or you think can be used to separate various related settings into different files?
Apparently, the existing enterprisey practice is that a developer creates a war and the ops department slaps it to the bluefish and takes care of all the database(and such) ops stuff (according to Jacob's email).
What dynamic settings.py
can you create that will aid the existing enterprise practices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我经常看到设置文件具有以下内容:
在
localsettings.py
中定义了诸如数据库连接和DEBUG
值之类的内容。localsettings.py
对于每个部署环境(开发/登台/生产等)来说是(或可能是)不同的,并且不与其他所有内容一起存在于源代码管理中。我最近发现有用的东西是将其放入我的
settings.py
中:在
default_localsettings.py
中,我定义了一堆默认值(DEBUG = True
>,使用与default_localsettings.py
位于同一目录中的 sqlite 数据库等)。一旦您完成设置,这可能就没用了,但我发现它很有用,只需能够将我的项目从源代码控制中检查出来并使用
runserver
立即运行,而无需设置任何东西。Often I've seen settings files with something like:
and in
localsettings.py
things like database connections andDEBUG
values are defined.localsettings.py
is (or may be) different for each deployment environment (dev/staging/production etc), and doesn't live in source control with everything else.Something I've found helpful lately is putting this in my
settings.py
:in
default_localsettings.py
I define a bunch of defaults (DEBUG = True
, use a sqlite database in the same directory asdefault_localsettings.py
etc).This might not be useful once you've got things set up, but I've found it useful just to be able to check my project out of source control and have it work straightaway using
runserver
, without having to set anything up.按照此设置覆盖示例来处理开发、登台和生产环境。
http://djangodose.com/articles/2009 /09/handling-development-staging-and-product-enviro/
(在 Wayback Machine 存档版本)
Follow this settings override example to handle dev, staging, and production environments.
http://djangodose.com/articles/2009/09/handling-development-staging-and-production-enviro/
(archived version at Wayback Machine)
这样的设置中创建一个模块
就我个人而言,我喜欢它从像文件
__init__.py
,如下所示:您已采用变量 BASE_DIR 来指向更高一级的目录。
使用此过程,您不必采用导入机制之类的任何其他内容。只需使用
来自 django.conf 导入设置
。 local.py 中的所有设置都会覆盖 settings.py 中的设置。然而,这种可能的解决方案可以与上面给出的其他答案相结合。
Personally, I like it to make a module out of settings like
The file
__init__.py
looks like:You have adopt the variable BASE_DIR to point to the directory one level higher.
Using this procedure, you don't have to adopt anything else like the import mechanism. Just use
from django.conf import settings
. All settings in local.py overwrite the ones in settings.py.However, this possible solution may be combined with the other answers given above.