I was wondering about whether it is better to import variables into your view from the settings.py file? or better to create a configuration file with the variables that are needed?
I tend to like to write configuration files for my Django applications, read, and import the variables from there when necessary. For example:
.configrc
[awesomeVariables]
someMagicNumber = 7
views.py
from ConfigParser import SafeConfigParser
#Open and parse the file
config = SafeConfigParser()
config.read(pathToConfig)
#Get variable
magicNumber = config.get('awesomeVariables', 'someMagicNumber'))
However, I’ve noticed some programmers prefer the following :
settings.py
SOME_MAGIC_NUMBER=7
views.py
import settings
magicNumber = settings.SOME_MAGIC_NUMBER
I was curious as to the pros and cons of the different methods? Can importing variables directly from your settings compromise the integrity of the architecture?
It’s a judgement call. Using the settings module is the “Django way”, although you should do
from django.conf import settings; settings.MY_SETTING, which will respectDJANGO_SETTINGS_MODULE. But Django is just Python; there’s nothing that will stop you from usingConfigParser. In the interest of having only one place where such things are defined, I’d recommend putting it in the Django settings file – but if you have a reason not to, don’t.