Arthur

Pemberton

Full-stack web applications developer


Sample settings.py for a Django microservice project

January 24, 2016Arthur Pemberton0 Comments

I present a sample settings.py for a Django project, setup to accomplish the following requirements.

    • requires no database
    • defaults to DEBUG=True but can be overridden with an environment variable
    • supports installation into a sub directory path via the SCRIPT_ALIAS environment variable
    • utilizes Django Rest Framework for API building
    • no session loading/saving

This leaves you with a fairly lightweight Django project which can be used as the basis of a microservice.

# -*- coding: utf-8 -*-

# stdlib imports
import os
import urlparse


# calculate base directory

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# handle environment variables

DEBUG = os.environ.get('DEBUG', 'True') ; assert(DEBUG.lower() in ('true','false')) ; DEBUG = DEBUG.lower() == 'true'
URL_BASE = os.environ.get('SCRIPT_NAME', '/').rstrip('/') + '/'


# Application definition

ADMINS = (
	('Example User', 'user@example.com'),
)

INSTALLED_APPS = [
	#'django.contrib.admin',
	#'django.contrib.auth',
	#'django.contrib.contenttypes',
	#'django.contrib.sessions',
	#'django.contrib.messages',
	'django.contrib.staticfiles',
	
	'rest_framework', # djangorestframework
	
	'microservice.apiv1',
]

MIDDLEWARE_CLASSES = [
	'django.middleware.security.SecurityMiddleware',
	#'django.contrib.sessions.middleware.SessionMiddleware',
	'django.middleware.common.CommonMiddleware',
	#'django.middleware.csrf.CsrfViewMiddleware',
	#'django.contrib.auth.middleware.AuthenticationMiddleware',
	#'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
	#'django.contrib.messages.middleware.MessageMiddleware',
	'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

APPEND_SLASH = False
USE_X_FORWARDED_HOST = True
USE_X_FORWARDED_PORT = True
ROOT_URLCONF = 'microservice.urls'
WSGI_APPLICATION = 'microservice.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = { }


# Password validation
# https://docs.djangoproject.com/en/1.9/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.9/topics/i18n/

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = False
USE_L10N = False 	
USE_TZ = True

# Mail

DEFAULT_FROM_EMAIL = 'app@example.com'
SERVER_EMAIL = 'app@example.com'


# Rest Framework
# http://www.django-rest-framework.org/api-guide/settings/

REST_FRAMEWORK = {
	'UNAUTHENTICATED_USER': None,
}


# Security
ALLOWED_HOSTS = ['*']
SECRET_KEY = '00000000000000000000000000000000000000000000000000' # CHANGE THIS


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

STATIC_URL = urlparse.urljoin(URL_BASE, 'static/')


# Templates

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',
			],
		},
	},
]

Leave a Reply