-1

Currently my manage.py file is hardcoded to import my local.py - development settings file. Is this the 'industry standard' way to set this up? When I deploy to the server do I just change manage.py to point to my production settings file? Or should I set this up another way?

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    # Hard coded imports local settings file
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.local")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Structure:

project/
   manage.py
   settings/
      local.py
      shared.py
      production.py
sazr
  • 24,984
  • 66
  • 194
  • 362
  • Possible duplicate of [Best way to handle different configuration/settings based on environment in Django project](http://stackoverflow.com/questions/40516873/best-way-to-handle-different-configuration-settings-based-on-environment-in-djan) – Moinuddin Quadri Dec 01 '16 at 21:46
  • This is among the first things said about `manage.py` in documentation (https://docs.djangoproject.com/en/1.10/ref/django-admin/#runserver): "DO NOT USE THIS SERVER IN A PRODUCTION SETTING". – Ivan Dec 01 '16 at 22:22

3 Answers3

2

No. manage.py has nothing whatsoever to do with running Django in production, so changing it won't help at all.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

I think you want to avoid editing manage.py if possible.

Another way to handle this is to use the default settings.py file, but to extend it using a second, local_settings.py file.

You can do this by putting the following at the end of your settings.py file:

locset = os.path.join(os.path.dirname(__file__), 'local_settings.py')
if os.path.exists(locset):
    with open(locset) as f:
        code = compile(f.read(), "local_settings.py", 'exec')
        exec(code)

I typically keep the DEBUG and database settings in this local_settings.py file.

When doing this, you should be sure to add local_settings.py to your .gitignore.

I also include an example version of this file alongside the settings.py file as local_settings.py.sample minus any sensitive password / username info.

This file is included in the repo so new folks can create their DB / user and just fill in the missing parts. They just need to rename it minus the .sample extension and they're good to go.

This is a simple and effective way to have variant settings for different environments, whether local, production or between local among team members.

Rob
  • 1,656
  • 2
  • 17
  • 33
0

My applications have one settings file, but the values are read from a config file instead of being hardcoded. For example, the DATABASES section looks like this:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('app.conf')

DATABASES = {
    'default': {
        'ENGINE': config.get('database', 'engine'),
        'NAME': config.get('database', 'name'),
        'USER': config.get('database', 'user'),
        'PASSWORD': config.get('database', 'password'),
        'HOST': config.get('database', 'host'),
        'PORT': config.get('database', 'port'),
    }
}

And the development and production servers each get their own app.conf file (which are excluded from version control as a nice side benefit).

John Gordon
  • 29,573
  • 7
  • 33
  • 58