I'm reading up that most people do from django.conf import settings but I don't undertstand the difference to simply doing import settings in a django project file. Can anyone explain the difference?
-
django.conf settings are django default or "global" settings which you may override with your own project based settings.Jingo– Jingo2013-11-14 11:16:17 +00:00Commented Nov 14, 2013 at 11:16
-
14NEVER EVER use the second form. The first one is the only correct one.bruno desthuilliers– bruno desthuilliers2013-11-14 12:03:43 +00:00Commented Nov 14, 2013 at 12:03
-
4But why never use the second one?tzenderman– tzenderman2013-11-14 13:42:27 +00:00Commented Nov 14, 2013 at 13:42
-
2possible duplicate of Django Importing Settings FileAnto– Anto2014-04-09 22:21:40 +00:00Commented Apr 9, 2014 at 22:21
2 Answers
import settings will import the first python module named settings.py found in sys.path. Usually (in default django setups) it allows access only to your site defined settings file, which overwrites django default settings (django.conf.global_settings).
So, if you try to access a valid django setting not specified in your settings file you will get an error.
django.conf.settings is not a file but an object (see source) making an abstraction of the concepts, default settings and your site-specific settings. Django also does other checks when you use from django.conf import settings.
You can also find it in the django docs.
3 Comments
import settings will import the first python module named settings.py found in sys.path. This may not be the "site defined settings file", which is looked up in the environment variable "DJANGO_SETTINGS_MODULE" and can be just any python package or module.settings.py next to manage.py (in the project root) is a default Django setup? O.o I believe the default one is to have it at <project-name>/settings.py. At least that's where django-admin startproject puts it.