2

I'm following the approach in Two Scoops of Django: Best Practices for Django 1.6 regarding multiple settings files. I'm using Django 1.7 and virtualenvwrapper.

My setup is as follows:

project/
    app1/
    app2/
    project/
        __init__.py
        settings/
            __init__.py
            base.py
            local.py
            production.py
    manage.py

I'm a bit confused as to how Django knows which settings file to use. I do not want to specify the settings file every time I run manage.py. I would rather like to set the DJANG_SETTINGS_MODULE environmental variable as explained in omouse anser here:

What confuses me is in the wsgi.py file there is a line:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.production")

Is this file only used in the production server? What happens if I already have a DJANGO_SETTINGS_MODULE environmental variable defined on the server?

When running it locally, I understand I need to set the DJANGO_SETTINGS_MODULE env variable every time I open the console. I've read here that I can define a postactivate hook in virtualenvwrapper. This hook will then create the environmental variables that I require everytime I activate the environment.

Is this the recommended way of ensuring the correct DJANGO_SETTINGS_MODULE env variable is loaded on my local machine? Would I also need to setup a similar file on my hosting server? I'm planning on using PythonAnywhere for hosting.

Lastly, if I run a staging server, how would I tell Django to load the staging settings file? The staging server is the practically the same as the production server, so I guess need a different wsgi.py file for the staging server, but that seems like a anti-pattern.

Community
  • 1
  • 1
Kritz
  • 7,099
  • 12
  • 43
  • 73

1 Answers1

2

os.environ.setdefault only sets the value if it is not set. When you run in production, export the environment variable DJANGO_SETTINGS_MODULE and set it to your production/staging settings file, and you don't have to set anything when running in development (if you set it by default to your development settings). This is the DRY-est method.

The method with a local_settings.py (which is most of the times kept out of the repo!) is not best practice and should be avoided.

Peter
  • 1,658
  • 17
  • 23
  • I definitely want all settings files in the repo. And do you agree the best approach to setting the DJANGO_SETTINGS_MODULE env variable locally is to create a postactivate hook in virtualenvwrapper? – Kritz Nov 01 '14 at 10:21
  • Locally you don't need to set the environment variable, since the default should be your local settings. On the production server, I recommend using the combination supervisor + gunicorn. Set the environment variable in a script that starts the application and you never have to set it again. – Peter Nov 01 '14 at 10:52
  • If the DJANGO_SETTINGS_MODULE env variable is not set locally, how does Django know which settings file to use? – Kritz Nov 01 '14 at 10:58
  • For development, the postactivate hook is cool, but other tools like honcho, foreman or envdir might help you. For production it is probably easier to set the variables using supervisord. See http://blog.doismellburning.co.uk/2014/10/06/twelve-factor-config-misunderstandings-and-advice/ – dukebody Nov 01 '14 at 11:02
  • @Johan in your WSGI file it would use the production file, while using manage.py it defaults to "{{projectname}}.settings". django-admin.py would probably fail. – dukebody Nov 01 '14 at 11:05