Skip to content

Instantly share code, notes, and snippets.

@bjinwright
Last active March 25, 2022 20:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bjinwright/c34b7cfece9911774276ca26bf2e73c0 to your computer and use it in GitHub Desktop.
Save bjinwright/c34b7cfece9911774276ca26bf2e73c0 to your computer and use it in GitHub Desktop.
Django Settings using Environment Variables via envs (https://github.com/bjinwright/envs) project.
DATABASE_ENGINE=django.db.backends.sqlite3
DATABASE_NAME=db.sqlite3
DATABASE_USER=
DATABASE_PASSWORD=
DATABASE_HOST=
DATABASE_PORT=
SECRET_KEY="asdfdfadsflkjsdflkjadsflkasfakl;"
STATIC_URL=/static/
DEBUG=True
"""
Django settings for project.
Generated by 'django-admin startproject' using Django 1.10.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
from unipath import FSPath as Path
from envs import env
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = Path(__file__).absolute().ancestor(1)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY','YourAppIsCool3423423')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG',False,var_type='boolean')
ALLOWED_HOSTS = ['.yourdomain.com','127.0.0.1','.execute-api.us-east-1.amazonaws.com']
CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
SITE_ID = env('SITE_ID',1,var_type='integer')
LOGIN_URL = env('LOGIN_URL','/admin/login/')
ROOT_URLCONF = 'yourblog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'yourblog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': env('DATABASE_ENGINE','django.db.backends.sqlite3'),
'NAME': env('DATABASE_NAME',os.path.join(BASE_DIR, 'db.sqlite3')),
'USER': env('DATABASE_USER'),
'PASSWORD':env('DATABASE_PASSWORD'),
'HOST':env('DATABASE_HOST'),
'PORT':env('DATABASE_PORT')
}
}
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://{0}".format(env('REDIS_HOST','localhost:6379')),
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
}
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_ROOT = PROJECT_PATH.child('static')
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = PROJECT_PATH.child('site_media')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME','ipoots-static')
AWS_S3_CUSTOM_DOMAIN = env('AWS_S3_CUSTOM_DOMAIN','ipoots-static.s3.amazonaws.com')
S3DIRECT_REGION = env('S3DIRECT_REGION','us-east-1')
STATICFILES_LOCATION = env('STATICFILES_LOCATION','yourblog-dev-static')
STATICFILES_STORAGE = env('STATICFILES_STORAGE','util.custom_storages.StaticStorage')
STATIC_URL = env('STATIC_URL','/yourblog-dev-static/')
MEDIAFILES_LOCATION = env('MEDIAFILES_LOCATION','yourblog-dev-media')
MEDIA_URL = env('MEDIA_URL','/yourblog-dev-media/')
DEFAULT_FILE_STORAGE = env('DEFAULT_FILE_STORAGE','util.custom_storages.MediaStorage')
S3DIRECT_DESTINATIONS = {
'post-images': {
'key': env('S3DIRECT_S3_KEY','{}/posts-static'.format(MEDIAFILES_LOCATION)),
'allowed': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
'auth': lambda u: u.is_staff,
},
}
AWS_HEADERS = {
'Cache-Control': 'max-age=1800',
}
{
"DEBUG":"False",
"DATABASE_USER":"yourdbuser",
"DATABASE_PASSWORD":"yourdbpassword",
"DATABASE_HOST":"yourdbhost.com",
"DATABASE_PORT":"3306",
"DATABASE_NAME":"yourdbname",
"DATABASE_ENGINE":"django.db.backends.mysql",
"SECRET_KEY":"yoursecretkey",
"AWS_ACCESS_KEY_ID":"yourawsaccesskey",
"AWS_SECRET_ACCESS_KEY":"yourawssecretkey",
"REDIS_HOST":"yourredishost.com:6379",
"STATIC_URL":"/yourblog-prod-static/",
"MEDIA_URL":"/yourblog-prod-media/",
"STATICFILES_LOCATION":"yourblog-prod-static",
"MEDIAFILES_LOCATION":"yourblog-prod-media"
}
@dafinoer
Copy link

dafinoer commented Dec 5, 2018

nicely 👍

@electrocnic
Copy link

This is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment