Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `from django.conf import settings` and `import settings` in a Django project

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?

like image 767
tzenderman Avatar asked Nov 14 '13 11:11

tzenderman


People also ask

What is from Django Conf import settings?

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.

Where is Django Conf settings?

Default settings These defaults live in the module django/conf/global_settings.py .

What is the purpose of settings py 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.

How do I import python settings?

In your python settings file, you have to import our LazySetting class located in python_settings. Only the first time you call this property, the HeavyInitializationClass will be instantiated and the *args and **kwargs parameters will be passed. Every time you call this property the same instance will be returned.


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.

Hope this helps.

like image 184
juliocesar Avatar answered Sep 26 '22 20:09

juliocesar


from django.conf import settings is better option.

I use different settings files for the same django project (one for "live", one for "dev"), the first one will select the one being executed.

like image 36
mansuetus Avatar answered Sep 25 '22 20:09

mansuetus