Skip to main content

Creating a Settings File for Development

The counterpart of the various changes we have made the previous section to make the project suitable for production is that we can no longer run it in development. This shouldn't be a problem, though. Typically, Django projects have multiple settings: one for development, one for production. So we are going to create a new file called settings_dev.py (next to settings.py) to put the specific configuration for the development environment that differs from the production one. Let's remember the values we changed in this chapter:

  • DEBUG
  • ALLOWED_HOSTS
  • DATABASES
  • STATIC_ROOT

Then go to the new file to reset that configuration to its development values:

# settings_dev.py
DEBUG = True
ALLOWED_HOSTS = []
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
STATIC_ROOT = None

We will now import this configuration when the file exists. Go to the end of settings.py and add the following block:

try:
# Import development configuration when the file is available.
from settings_dev import *
except ModuleNotFoundError:
# If the file does not exist, we are running in production.
pass

We are never uploading the settings_dev.py file to PythonAnywhere. However, when running python manage.py runserver locally, the file will be present and the variables configured in it will replace those previously defined in settings.py. That's why we import the module at the end of the file and not at the beginning as usual.