It is sometimes beneficial to share certain constants between various code files in a django application.
在django应用程序*享各种代码文件之间的某些常量有时是有益的。
Examples:
- Name or location of dump file used in various modules\commands etc
- Debug mode on\off for the entire app
- Site specific configuration
示例: - 各种模块\命令等中使用的转储文件的名称或位置 - 整个应用程序的\ off上的调试模式 - 站点特定配置
What would be the elegant\pythonic way of doing this?
这样做的优雅\ pythonic方式是什么?
4 个解决方案
#1
6
There's already a project-wide settings.py
file. This is the perfect place to put your own custom setttings.
已有一个项目范围的settings.py文件。这是进行自定义设置的理想场所。
#2
6
you can provide settings in your settings.py like
你可以在settings.py中提供设置
MY_SETTING = 'value'
and in any module you can fetch it like
在任何模块中你都可以像它一样获取它
from django.conf import settings
settings.MY_SETTING
#3
0
Create a configuration module.
创建配置模块。
Configuration.py: (in your project/app source directory)
Configuration.py :(在您的项目/应用程序源目录中)
MYCONST1 = 1
MYCONST2 = "rabbit"
Import it from other source files:
从其他源文件导入它:
from Configuration import MYCONST1,MYCONST2
...
#4
0
Django apps are meant to be (more or less) pluggable. Therefore, you are not supposed to hack into the code of an app in order to parametrize what you want (it would be quite a mess if you had to do this ! Imagine you want to upgrade an app you downloaded on internet... you would have to re-hack into the code of the new version ?!?).
Django应用程序应该(或多或少)可插拔。因此,你不应该破解应用程序的代码以便参数化你想要的东西(如果你不得不这样做会很麻烦!想象你想要升级你在互联网上下载的应用程序......你将不得不重新入侵新版本的代码?!?)。
For this reason you shouldn't add app-level settings at the app level, but rather put them together somewhere in your project-wide settings.py
.
因此,您不应在应用级别添加应用级设置,而是将它们放在项目范围的settings.py中的某个位置。
#1
6
There's already a project-wide settings.py
file. This is the perfect place to put your own custom setttings.
已有一个项目范围的settings.py文件。这是进行自定义设置的理想场所。
#2
6
you can provide settings in your settings.py like
你可以在settings.py中提供设置
MY_SETTING = 'value'
and in any module you can fetch it like
在任何模块中你都可以像它一样获取它
from django.conf import settings
settings.MY_SETTING
#3
0
Create a configuration module.
创建配置模块。
Configuration.py: (in your project/app source directory)
Configuration.py :(在您的项目/应用程序源目录中)
MYCONST1 = 1
MYCONST2 = "rabbit"
Import it from other source files:
从其他源文件导入它:
from Configuration import MYCONST1,MYCONST2
...
#4
0
Django apps are meant to be (more or less) pluggable. Therefore, you are not supposed to hack into the code of an app in order to parametrize what you want (it would be quite a mess if you had to do this ! Imagine you want to upgrade an app you downloaded on internet... you would have to re-hack into the code of the new version ?!?).
Django应用程序应该(或多或少)可插拔。因此,你不应该破解应用程序的代码以便参数化你想要的东西(如果你不得不这样做会很麻烦!想象你想要升级你在互联网上下载的应用程序......你将不得不重新入侵新版本的代码?!?)。
For this reason you shouldn't add app-level settings at the app level, but rather put them together somewhere in your project-wide settings.py
.
因此,您不应在应用级别添加应用级设置,而是将它们放在项目范围的settings.py中的某个位置。