際際滷

際際滷Share a Scribd company logo
Settings in Django
Applications
Handling of settings in various environments
Tom叩邸 Sirn箪
@junckritter
Default
 1 file - settings.py
 good for local development
 deploy to production?
 different environments / developer machines
(OSX/Win)
 to git or not to git?
Simple solution
 if-clause for name of production/devel machine
 magic with imports from different files
 manually curated file on production machine
Problems
 Hard to manage different sets of settings
 Copying of files/code snippets in deploy scripts,
test/build configs
 Not replicable if not versioned
 No recommendation in Django documentation
The right way
 based on https://speakerdeck.com/jacobian/the-
best-and-worst-of-django?slide=81 (by Djangos
contributor)
 Two Scoops of Django
Settings in Django application
Principles
 common parts to base.py
 separate file for each environment
 use env variable DJANGO_SETTINGS_MODULE
or CLI parameter --settings= for ./manage.py
 everything is in git
 easy additions/changes of particular setting values
Our setup
 __init__.py
 base.py
 helpers.py
 local.tpl.py
 admin.py
 staging.py
 production.py
base.py
 sane defaults - application could (mostly) run as-is
 INSTALLED_APPS, DEBUG, DATABASES,
CACHES
 settings for applications
helpers.py
 code running at the end
 generate final settings based on temporary ones
DEBUG_MODULES = ['action', emails] in local.py
for module in globals().get('DEBUG_MODULES', []):
LOGGING[loggers][module] =
local.tpl.py
 template for local development
 good starting point for newcomer
 good place to manifest changes / new settings,
visible in VCS diff
[environment].py
 from .base import *
 
 execfile(os.path.join(PROJECT_PATH,
'project/settings/helpers.py
[environment].py
 only changes for particular environment
 diff against base.py (or Djangos global settings)
 $DJANGO_SETTINGS_MODULE=[environment].py
Pros
 More control
 Easy check/reference to particular setting
 Easier build/deployment
 Documented changes/defaults
Cons
 Possible duplication for similar environments
(admin, production) - more layers, shared parts
 env variable have to be set everywhere
Tips & tricks
Settings for applications
 settings file is big chunk of code
 all settings in one (or few) place(s)
 harder to find settings for particular application
Our solution
 settings.py in each application with defaults
 app1/settings.py
 app2/settings.py
 global settings file can override
 APP_SETTING =
settings.get(GLOBAL_APP_SETTING, default)
Cache invalidation
 hard problem in CS
 especially during deployment/migration
 format of key is usually tied to version of code
Cache invalidation
in base.py
from subprocess import check_output, CalledProcessError
try:
VERSION = check_output(
'git rev-parse --short=8 HEAD',
shell=True
).strip()
except CalledProcessError:
VERSION = 'test'
Cache invalidation
in production.py
CACHES = {
'default': {
'BACKEND': 'project.cache.EnhancedRedisCache',
'LOCATION': 'redis:6379',
'VERSION': VERSION,
'OPTIONS': {
'DB': 1,
'PASSWORD': '',
'PARSER_CLASS': 'redis.connection.HiredisParser'
}
}
}
One more thing
Migrate Postgres
psql -h <host1> ... -c 'copy (select ... from
<tablename> where ) to stdout' |
psql -h <host2> ... -c 'copy <tablename> from stdin'
Ad

Recommended

Package management and creation in Gentoo Linux
Package management and creation in Gentoo Linux
Donnie Berkholz
Puppetize PDX 2019 - Automated Patching with Bolt by Nick Maludy
Puppetize PDX 2019 - Automated Patching with Bolt by Nick Maludy
Nick Maludy
Git 螳 覦 覯
Git 螳 覦 覯
Lee Yongmin
Senior Service Consultant for St Louis Area West (formal proposal 6.0)-email ...
Senior Service Consultant for St Louis Area West (formal proposal 6.0)-email ...
Ryan Barden
School Management System ppt
School Management System ppt
Mohsin Ali
From Zero to Hero @ PyGrunn 2014
From Zero to Hero @ PyGrunn 2014
meij200
7. Project Directory Structure and..pptx
7. Project Directory Structure and..pptx
BahubalSingh
Django Best Practices
Django Best Practices
Abdullah etin AVDAR
Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]
Devon Bernard
Django dev-env-my-way
Django dev-env-my-way
Robert Lujo
Fabric Python Lib
Fabric Python Lib
Simone Federici
Django Deployment with Fabric
Django Deployment with Fabric
Jonas Nockert
Django deployment best practices
Django deployment best practices
Erik LaBianca
Introduction to django-config
Introduction to django-config
Tareque Hossain
Django Hosting
Django Hosting
Derek Stegelman
Secure your Secrets and Settings in ColdFusion
Secure your Secrets and Settings in ColdFusion
Ortus Solutions, Corp
How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]
Devon Bernard
Systems administration for coders presentation
Systems administration for coders presentation
Matt Willsher
Automation - fabric, django and more
Automation - fabric, django and more
Ilian Iliev
Developing In Python On Red Hat Platforms (Nick Coghlan & Graham Dumpleton)
Developing In Python On Red Hat Platforms (Nick Coghlan & Graham Dumpleton)
Red Hat Developers
Developing in Python on Red Hat Platforms (DevNation 2016)
Developing in Python on Red Hat Platforms (DevNation 2016)
ncoghlan_dev
What the heck went wrong?
What the heck went wrong?
Andy McKay
Python Versions and Dependencies Made Easy
Python Versions and Dependencies Made Easy
Sebastian Witowski
Pitfalls of Continuous Deployment
Pitfalls of Continuous Deployment
zeeg
Clearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad Decisions
Jonathan Hitchcock
The Best (and Worst) of Django
The Best (and Worst) of Django
Jacob Kaplan-Moss
Tools for Development and Debugging in Python
Tools for Development and Debugging in Python
zeeg
Use of django at jolt online v3
Use of django at jolt online v3
Jaime Buelta
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot

More Related Content

Similar to Settings in Django application (20)

Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]
Devon Bernard
Django dev-env-my-way
Django dev-env-my-way
Robert Lujo
Fabric Python Lib
Fabric Python Lib
Simone Federici
Django Deployment with Fabric
Django Deployment with Fabric
Jonas Nockert
Django deployment best practices
Django deployment best practices
Erik LaBianca
Introduction to django-config
Introduction to django-config
Tareque Hossain
Django Hosting
Django Hosting
Derek Stegelman
Secure your Secrets and Settings in ColdFusion
Secure your Secrets and Settings in ColdFusion
Ortus Solutions, Corp
How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]
Devon Bernard
Systems administration for coders presentation
Systems administration for coders presentation
Matt Willsher
Automation - fabric, django and more
Automation - fabric, django and more
Ilian Iliev
Developing In Python On Red Hat Platforms (Nick Coghlan & Graham Dumpleton)
Developing In Python On Red Hat Platforms (Nick Coghlan & Graham Dumpleton)
Red Hat Developers
Developing in Python on Red Hat Platforms (DevNation 2016)
Developing in Python on Red Hat Platforms (DevNation 2016)
ncoghlan_dev
What the heck went wrong?
What the heck went wrong?
Andy McKay
Python Versions and Dependencies Made Easy
Python Versions and Dependencies Made Easy
Sebastian Witowski
Pitfalls of Continuous Deployment
Pitfalls of Continuous Deployment
zeeg
Clearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad Decisions
Jonathan Hitchcock
The Best (and Worst) of Django
The Best (and Worst) of Django
Jacob Kaplan-Moss
Tools for Development and Debugging in Python
Tools for Development and Debugging in Python
zeeg
Use of django at jolt online v3
Use of django at jolt online v3
Jaime Buelta
Effective Python Package Management [PyCon Canada 2017]
Effective Python Package Management [PyCon Canada 2017]
Devon Bernard
Django dev-env-my-way
Django dev-env-my-way
Robert Lujo
Django Deployment with Fabric
Django Deployment with Fabric
Jonas Nockert
Django deployment best practices
Django deployment best practices
Erik LaBianca
Introduction to django-config
Introduction to django-config
Tareque Hossain
Secure your Secrets and Settings in ColdFusion
Secure your Secrets and Settings in ColdFusion
Ortus Solutions, Corp
How to Design a Great API (using flask) [ploneconf2017]
How to Design a Great API (using flask) [ploneconf2017]
Devon Bernard
Systems administration for coders presentation
Systems administration for coders presentation
Matt Willsher
Automation - fabric, django and more
Automation - fabric, django and more
Ilian Iliev
Developing In Python On Red Hat Platforms (Nick Coghlan & Graham Dumpleton)
Developing In Python On Red Hat Platforms (Nick Coghlan & Graham Dumpleton)
Red Hat Developers
Developing in Python on Red Hat Platforms (DevNation 2016)
Developing in Python on Red Hat Platforms (DevNation 2016)
ncoghlan_dev
What the heck went wrong?
What the heck went wrong?
Andy McKay
Python Versions and Dependencies Made Easy
Python Versions and Dependencies Made Easy
Sebastian Witowski
Pitfalls of Continuous Deployment
Pitfalls of Continuous Deployment
zeeg
Clearly, I Have Made Some Bad Decisions
Clearly, I Have Made Some Bad Decisions
Jonathan Hitchcock
The Best (and Worst) of Django
The Best (and Worst) of Django
Jacob Kaplan-Moss
Tools for Development and Debugging in Python
Tools for Development and Debugging in Python
zeeg
Use of django at jolt online v3
Use of django at jolt online v3
Jaime Buelta

Recently uploaded (20)

Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
Artificial Intelligence Workloads and Data Center Management
Artificial Intelligence Workloads and Data Center Management
SandeepKS52
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Tech Services
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
Software Testing & its types (DevOps)
Software Testing & its types (DevOps)
S Pranav (Deepu)
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
Reimagining Software Development and DevOps with Agentic AI
Reimagining Software Development and DevOps with Agentic AI
Maxim Salnikov
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
Artificial Intelligence Workloads and Data Center Management
Artificial Intelligence Workloads and Data Center Management
SandeepKS52
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Tech Services
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
Software Testing & its types (DevOps)
Software Testing & its types (DevOps)
S Pranav (Deepu)
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
Reimagining Software Development and DevOps with Agentic AI
Reimagining Software Development and DevOps with Agentic AI
Maxim Salnikov
Best MLM Compensation Plans for Network Marketing Success in 2025
Best MLM Compensation Plans for Network Marketing Success in 2025
LETSCMS Pvt. Ltd.
Ad

Settings in Django application

  • 1. Settings in Django Applications Handling of settings in various environments Tom叩邸 Sirn箪 @junckritter
  • 2. Default 1 file - settings.py good for local development deploy to production? different environments / developer machines (OSX/Win) to git or not to git?
  • 3. Simple solution if-clause for name of production/devel machine magic with imports from different files manually curated file on production machine
  • 4. Problems Hard to manage different sets of settings Copying of files/code snippets in deploy scripts, test/build configs Not replicable if not versioned No recommendation in Django documentation
  • 5. The right way based on https://speakerdeck.com/jacobian/the- best-and-worst-of-django?slide=81 (by Djangos contributor) Two Scoops of Django
  • 7. Principles common parts to base.py separate file for each environment use env variable DJANGO_SETTINGS_MODULE or CLI parameter --settings= for ./manage.py everything is in git easy additions/changes of particular setting values
  • 8. Our setup __init__.py base.py helpers.py local.tpl.py admin.py staging.py production.py
  • 9. base.py sane defaults - application could (mostly) run as-is INSTALLED_APPS, DEBUG, DATABASES, CACHES settings for applications
  • 10. helpers.py code running at the end generate final settings based on temporary ones DEBUG_MODULES = ['action', emails] in local.py for module in globals().get('DEBUG_MODULES', []): LOGGING[loggers][module] =
  • 11. local.tpl.py template for local development good starting point for newcomer good place to manifest changes / new settings, visible in VCS diff
  • 12. [environment].py from .base import * execfile(os.path.join(PROJECT_PATH, 'project/settings/helpers.py
  • 13. [environment].py only changes for particular environment diff against base.py (or Djangos global settings) $DJANGO_SETTINGS_MODULE=[environment].py
  • 14. Pros More control Easy check/reference to particular setting Easier build/deployment Documented changes/defaults
  • 15. Cons Possible duplication for similar environments (admin, production) - more layers, shared parts env variable have to be set everywhere
  • 17. Settings for applications settings file is big chunk of code all settings in one (or few) place(s) harder to find settings for particular application
  • 18. Our solution settings.py in each application with defaults app1/settings.py app2/settings.py global settings file can override APP_SETTING = settings.get(GLOBAL_APP_SETTING, default)
  • 19. Cache invalidation hard problem in CS especially during deployment/migration format of key is usually tied to version of code
  • 20. Cache invalidation in base.py from subprocess import check_output, CalledProcessError try: VERSION = check_output( 'git rev-parse --short=8 HEAD', shell=True ).strip() except CalledProcessError: VERSION = 'test'
  • 21. Cache invalidation in production.py CACHES = { 'default': { 'BACKEND': 'project.cache.EnhancedRedisCache', 'LOCATION': 'redis:6379', 'VERSION': VERSION, 'OPTIONS': { 'DB': 1, 'PASSWORD': '', 'PARSER_CLASS': 'redis.connection.HiredisParser' } } }
  • 23. Migrate Postgres psql -h <host1> ... -c 'copy (select ... from <tablename> where ) to stdout' | psql -h <host2> ... -c 'copy <tablename> from stdin'