less than 1 minute read

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?

  1. Create a folder named settings
    # at the project directory and the project name is 'mysite'
    $ mkdir mysite/settings 
    
  2. Move the original settings file and change its name
    $ mv mysite/settings.py mysite/settings/base.py
    
  3. Create a new settings file
    $ touch mysite/settings/dev_env.py
    
  4. Create init.py in the settings folder.
    # This makes the Django recognize the settings folder 
    # as a python package. 
    $ touch mysite/settings/__init__.py
    
  5. Instead of copying all relative settings override required changes. Please checkout this commit.

  6. 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