Separating settings files in Django
This post is followed by the article. To check it out changes, please review this commit.
Why do we need to separate settings files in Django?
In my case, when I collaborate with other developers, I feel a separation of settings file is required. Let’s say I use Mac and a colleague uses Windows. In that case, it is necessary to maintain two settings files. Or, if I want to test it in production mode in Django, then I don’t need to set DEBUG but in a development environment. I will turn on the DEBUG setting to find an error.
How to separate it?
- Create a folder named settings
# at the project directory and the project name is 'mysite' $ mkdir mysite/settings
- Move the original settings file and change its name
$ mv mysite/settings.py mysite/settings/base.py
- Create a new settings file
$ touch mysite/settings/dev_env.py
- Create init.py in the settings folder.
# This makes the Django recognize the settings folder # as a python package. $ touch mysite/settings/__init__.py
-
Instead of copying all relative settings override required changes. Please checkout this commit.
- Execute Django with settings option
# make sure you are in pipenv shell (pipenv_name) $ python manage.py runserver --settings=mysite.settings.dev_env
Leave a comment