Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing settings in Django

Tags:

django

I am working on Django project that uses several dozen configuration variables specified in several "settings" files located in project's root directory:

--> myproject
------> app folders
------> ...
--- settings.py
--- settings_global.py
--- settings_production.py
--- settings_development.py

Variables from different settings_* files are then get imported in settings.py file based on certain run-time parameters (host name etc). It all works rather well, but sometimes it's still hard to locate certain variable, so I'd like re-organize settings variables and split them into several categories:

  • project-specific variables
  • django-specific variables
  • installed-app specific variables (such as settings for django_compressor, etc)
  • environment-specific variables (production/development)

Also I'd like to move all settings files but settings.py file to settings subdirectory:

--> myproject
------> app folders
------> ...
------> settings
---------- __init__.py
---------- common.py
---------- production.py
---------- development.py
---------- apps.py
---------- ...
--- settings.py

I've created settings subdirectory (as well as empty __init__.py file) and copied/renamed the settings files. Then I tried to import those variables in my setting.py file as following:

from settings.common import *
from settings.apps import *

However, I am getting the following error (even though ROOT_URLCONF exists in settings/common.py file):

AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

What am I doing wrong?

like image 767
MikeAr Avatar asked Apr 10 '12 07:04

MikeAr


People also ask

Where are Django settings stored?

A Django settings file doesn't have to define any settings if it doesn't need to. Each setting has a sensible default value. These defaults live in the module django/conf/global_settings.py .

What is settings PY in Django?

Insights about settings.py file. A Django settings file contains all the configuration of your Django Project. In this article the important points of settings.py file of Django will be discussed. A settings file is just a Python module with module-level variables.

How do I use .ENV in Django?

Creating Environment Variables Create a . env file in the same directory where settings.py resides and add the following key-value pair inside the file. We will use django-environ for managing environment variables inside our project. So let's install the package.


1 Answers

I think there's a name collision between your settings.py module and the settings package, try renaming the package to something else

like image 140
Claude Vedovini Avatar answered Oct 06 '22 17:10

Claude Vedovini