Django - How to share configuration constants within an app?
It is sometimes beneficial to share certain constants between various code files in 开发者_StackOverflowa django application.
Examples:
- Name or location of dump file used in various modules\commands etc - Debug mode on\off for the entire app - Site specific configurationWhat would be the elegant\pythonic way of doing this?
There's already a project-wide settings.py
file. This is the perfect place to put your own custom setttings.
you can provide settings in your settings.py like
MY_SETTING = 'value'
and in any module you can fetch it like
from django.conf import settings
settings.MY_SETTING
Create a configuration module.
Configuration.py: (in your project/app source directory)
MYCONST1 = 1
MYCONST2 = "rabbit"
Import it from other source files:
from Configuration import MYCONST1,MYCONST2
...
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 ?!?).
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
.
精彩评论