I have been developing a basic app. Now at the deployment stage it has become clear I have need for both a local settings and production settings.
It would be great to know the following:
- How best to deal with development and production settings.
- How to keep apps such as django-debug-toolbar only in a development environment.
- Any other tips and best practices for development and deployment settings.
The
DJANGO_SETTINGS_MODULEenvironment variable controls which settings file Django will load.You therefore create separate configuration files for your respective environments (note that they can of course both
import *from a separate, “shared settings” file), and useDJANGO_SETTINGS_MODULEto control which one to use.Here’s how:
As noted in the Django documentation:
So, let’s assume you created
myapp/production_settings.pyandmyapp/test_settings.pyin your source repository.In that case, you’d respectively set
DJANGO_SETTINGS_MODULE=myapp.production_settingsto use the former andDJANGO_SETTINGS_MODULE=myapp.test_settingsto use the latter.From here on out, the problem boils down to setting the
DJANGO_SETTINGS_MODULEenvironment variable.Setting
DJANGO_SETTINGS_MODULEusing a script or a shellYou can then use a bootstrap script or a process manager to load the correct settings (by setting the environment), or just run it from your shell before starting Django:
export DJANGO_SETTINGS_MODULE=myapp.production_settings.Note that you can run this export at any time from a shell — it does not need to live in your
.bashrcor anything.Setting
DJANGO_SETTINGS_MODULEusing a Process ManagerIf you’re not fond of writing a bootstrap script that sets the environment (and there are very good reasons to feel that way!), I would recommend using a process manager:
environmentconfiguration key..env) file.Finally, note that you can take advantage of the
PYTHONPATHvariable to store the settings in a completely different location (e.g. on a production server, storing them in/etc/). This allows for separating configuration from application files. You may or may not want that, it depends on how your app is structured.