-
Notifications
You must be signed in to change notification settings - Fork 2
Settings and Configuration
SOS Django Template, hence its name (Separation of Settings), separates development and production settings. The setting files are located in project.settings
module and structured as below:
-
project.settings.defaults
module: This module usually contains default settings that are generated withdjango-admin startproject
and some defaults of SOS Django Template. It is not meant to be changed by hand, should be left as it is. -
project.settings.base
module: This module contains the settings in both production and development environment. A best point where you can define the settings that should be in both development and production environment. -
project.settings.development
module only contains settings for development environment. -
project.settings.production
module only contains settings for production environment.
These modules import all from parents (e.g. from .base import *
) in a hierarchy shown as below:
defaults
└── base
├── development
└── production
So, base
module imports everything in defaults
. development
imports base
and production
imports base
as well, but not each other.
Usually, when you run python3 manage.py runserver
, development
module is used because you won't do that in production environment. On the other hand, project.wsgi
module uses production
module.
Recommended reading: 12factor Conventions
Utilizing a .env
file has become a standard and used in many web frameworks. It helps you migrate your project to many environment with less hassle. .env
file itself is just an environment variable file where you can define your environment varibles.
SOS Django Template also has .env.example
file and you need to rename it to .env
at the beginning of your project. This file is ignored by git and you shouldn't push it to your repository anyway.
SOS Django Template already has predefined variables for you to setup, these can be seen in the table below:
Variable Name | Related Django Settings Variable | Default in File | Default if not defined | Description |
---|---|---|---|---|
DJANGO_SECRET_KEY |
SECRET_KEY |
- | ERROR | Secret key to encrypt data in database. |
DJANGO_DEBUG |
DEBUG |
true |
True |
Whether to run Django in debug mode or not. |
DJANGO_DB_ENGINE |
DATABASES["default"]["ENGINE"] |
"django.db.backends.postgresql" |
"django.db.backends.sqlite3" |
Which database engine to use. |
DJANGO_DB_NAME |
DATABASES["default"]["NAME"] |
"project" |
os.path.join(BASE_DIR, "db.sqlite3") |
Database name. |
DJANGO_DB_USER |
DATABASES["default"]["USER"] |
"postgres" |
"postgres" |
Database username. |
DJANGO_DB_PASSWORD |
DATABASES["default"]["PASSWORD"] |
"" |
"" |
Database password. |
DJANGO_DB_HOST |
DATABASES["default"]["HOST"] |
"127.0.0.1" |
"127.0.0.1" |
Database host. |
DJANGO_DB_PORT |
DATABASES["default"]["PORT"] |
5432 |
5432 |
Database port. |
DJANGO_LANGUAGE_CODE |
LANGUAGE_CODE |
"en-us" |
"en-us" |
Default language code. |
DJANGO_TIME_ZONE |
TIME_ZONE |
"UTC" |
"UTC" |
Default time zone. |
DJANGO_USE_I18N |
USE_I18N |
true |
True |
Whether to use internalization module of Django. |
DJANGO_USE_L10N |
USE_L10N |
true |
True |
Whether to use localization module of Django. |
DJANGO_USE_TZ |
USE_TZ |
true |
True |
Whether to use timezones while storing datetimes in database. |
DJANGO_STATIC_URL |
STATIC_URL |
"/static/" |
"/static/" |
URL to serve static files. |
DJANGO_STATIC_ROOT |
STATIC_ROOT |
- |
django dir under temporary directory in your system in development environment |
Where to collect all static files. |
Since you don't push your .env
file to the repository, while setting up production server, you can just copy your .env
file to production server and make some adjustments.
If you'd like your settings to be present in development environment, you can edit project.settings.development
module, and for the production-specific settings, you can use projects.settings.production
module. If you want your settings to be present in both environment, you can edit projects.settings.base
module.
While installing and configuring a 3rd-party app, you usually install it first and add it into INSTALLED_APPS
variable. While this is a way to do that, (i) it is better to leave project.settings.default
as is and (ii) add settings into its own section. Some apps already have been defined in one of those settings module in SOS Django Template. For example, see base module. It has a specific section for Django Rest Framework:
# Rest Framework
INSTALLED_APPS.append("rest_framework")
As you can see, instead of changing project.settings.defaults
, I directly append
to INSTALLED_APPS
array inside of it in project.settings.base
module. If I want further customization for Django Rest Framework, it would be better to add it under its section.
# Rest Framework
INSTALLED_APPS.append("rest_framework")
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
],
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
]
}
So each time you install an app, it is better to create its own section whether in base
, development
and/or production
module and add it in there.