views:

48

answers:

4

It is sometimes beneficial to share certain constants between various code files in a 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 configuration

What would be the elegant\pythonic way of doing this?

A: 

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
...
adamk
+5  A: 

There's already a project-wide settings.py file. This is the perfect place to put your own custom setttings.

Daniel Roseman
Indeed, that's why I specifically asked regarding application scope
Jonathan
@Jonathan: Are you looking for an *app specific* settings file? Something that can be later overridden in a project settings file?
Manoj Govindan
@Jonathan, you can/should put app specific settings in settings.py, ultimately a project is run on server not app itself
Anurag Uniyal
+1  A: 

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
Tumbleweed
A: 

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.

sebpiq