Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : is it better to import variables from a settings.py file, or basic configuration file?

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?

like image 521
mshell_lauren Avatar asked Jan 13 '12 19:01

mshell_lauren


People also ask

What is the use of settings py file in Django?

settings.py is a core file in Django projects. It holds all the configuration values that your web app needs to work; database settings, logging configuration, where to find static files, API keys if you work with external APIs, and a bunch of other stuff.

Where do I put settings py?

Create a settings.py file in your application's package (for example, if your application is named "myapp", put it in the filesystem directory named myapp ; the one with an __init__.py in it.

What is the typical function of settings py in Django web development?

settings.py contains all the website settings, including registering any applications we create, the location of our static files, database configuration details, etc.


3 Answers

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 respect DJANGO_SETTINGS_MODULE. But Django is just Python; there's nothing that will stop you from using ConfigParser. 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.

like image 179
AdamKG Avatar answered Oct 01 '22 19:10

AdamKG


Using a config file is completely un-Django. Settings go in settings.py. Period. For app-specific settings, you simply set a default in your app and allow the user to override in their project's settings.py:

from django.conf import settings

SOME_MAGIC_NUMBER = settings.SOME_MAGIC_NUMBER if hasattr(settings, 'SOME_MAGIC_NUMBER') else 0
# Where `0` is the default value
like image 36
Chris Pratt Avatar answered Oct 01 '22 21:10

Chris Pratt


There also an app for storing settings dynamically in db. Maybe you find it usefull . django-constance

like image 25
Aldarund Avatar answered Oct 01 '22 20:10

Aldarund