Skip to content

Instantly share code, notes, and snippets.

@wastemobile
Last active February 29, 2020 15:49
Show Gist options
  • Save wastemobile/2e5fa7d58eadfeac926a9295c3fdf7a9 to your computer and use it in GitHub Desktop.
Save wastemobile/2e5fa7d58eadfeac926a9295c3fdf7a9 to your computer and use it in GitHub Desktop.
Django settings
# dev
SECRET_KEY=4(hf^my1$a*dwf^78^x0zl-z)*)p&)40z#o307o*)p_x2ker^v
DEBUG=True
ALLOWED_HOSTS=.localhost,127.0.0.1
# for PostgreSQL
# DATABASE_URL=postgres://dba:password@localhost:5432/db
# for Gmail
# EMAIL_BACKEND="django.core.mail.backends.console.EmailBackend"
# EMAIL_HOST="smtp.gmail.com"
# EMAIL_PORT="587"
# EMAIL_USE_TLS=True
# EMAIL_USER="[email protected]"
# EMAIL_PASS="app-password"
  1. pipenv install python-decouple dj-database-url
  2. 建立 .env
  3. 修改 settings.py

python-decouple

import os
from decouple import config, Csv
from dj_database_url import parse as db_url
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())
DATABASES = {
'default': config(
'DATABASE_URL',
default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3'),
cast=db_url
)
}
LANGUAGE_CODE = 'zh-Hant'
TIME_ZONE = 'Asia/Taipei'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'asset')]
EMAIL_BACKEND = config('EMAIL_BACKEND', default='django.core.mail.backends.smtp.EmailBackend')
EMAIL_HOST = config('EMAIL_HOST', default='localhost')
EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int)
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default='')
EMAIL_HOST_USER = config('EMAIL_HOST_USER', default='')
EMAIL_USE_TLS = config('EMAIL_USE_TLS', default=False, cast=bool)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment