SlideShare a Scribd company logo
12 tips on Django Best Practices
David Arcos
catchoom.com | @catchoom
12 tips on Django Best Practices
Some (personal) suggestions on:
- Development
- Deployment
- External tools

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Hi! I'm David Arcos
- Python/Django developer
- discovered Django in 2007 (v0.96)
- professionally since 2008

- Web backend, distributed systems,
databases, scalability, security
- Team leader at Catchoom

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices

- Image Recognition SaaS
- Top recognition results, shortest response times
- Easy to integrate into your apps/services

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Why Python? PEP 20, The Zen of Python:
- “Beautiful is better than ugly”
- “Simple is better than complex”
- “Complex is better than complicated”
- “Readability counts”
- “Special cases aren't special enough to break the rules”
- “If the implementation is hard to explain, it's a bad idea”
- (…)
http://www.python.org/dev/peps/pep-0020/

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Why Django?
- "The Web framework for perfectionists with deadlines"
- Django design philosophy:

- loose coupling, less code, DRY, consistency, etc...
- https://docs.djangoproject.com/en/dev/misc/design-philosophies/

- technically: tons of django apps, very good doc

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Virtualenv:
- “one project, one virtualenv”
- projects with different dependencies, package versions
- easier to deploy. Forget dependency hell!
- virtualenvwrapper is a convenient tool

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Dependencies:
- use pip:
pip install catchoom
- save the dependencies in a requirements.txt file:
pip freeze > requirements.txt
pip install -r requirements.txt
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Layout: projects and apps
- project = the full website. app = python library
repository/
|-- doc
`-- project
|-- apps
|
|-- app1
|
|-- app2
|
`-- app3
`-- settings
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices

- use short, obvious, single-word names for your apps
- many small apps is better than a few giant apps:
- explain an app in a sentence. If you can't, split the app
- rather than expand an app, write a new app
- don't reinvent the wheel!
- django.contrib
- 3rd-party apps
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Settings:
- multiple settings files:
- per environment: dev, testing, staging, production
- per developer (local settings, use the hostname)
- all settings files must inherit from base, so you can do:
INSTALLED_APPS += ('debug_toolbar', )

- version control all the settings!
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Django is a MTV framework
- Model (app/models.py)
- Template (app/templates/*.html)
- View (app/views.py)

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Fat models, thin views...
- logic should go to the models (and forms, signals...)
- keep the views at a minimum
Good example: django.contrib.auth.models.User

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices

- why? Because of maintainability!
- a model is much easier to test
- reusable logic: form validation, signals, etc
- the code becomes clearer, more self-documenting

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
...and stupid templates!
- your template layer should be as thin as possible
- (by design) django templates are limited, constrained
- doesn't fit your use case? Use jinja2 in those views
- Hey, but I get ugly generated HTML!
- doesn't matter, you want maintainable templates

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Deployment:
- web server:
- Nginx + gunicorn
- Supervisord to keep it alive.
- static server:
- Nginx. Or any CDN.
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Fabric:
"a library and command-line tool for streamlining the use of SSH for
application deployment or systems administration tasks"

- to automate deployments, migrations, execute
management commands, monitoring...
- no more repetitive maintainance tasks done manually!

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
South:
"intelligent schema and data migrations for Django projects"

- creates migration files automatically.
- You can still do changes
- can do backward migrations
- will avoid disasters. Use it!
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Celery:
"asynchronous task queue/job queue based on distributed message passing"

- execute tasks asynchronously, in a pool workers
- cpu-intensive or I/O-intensive tasks:
- emails, pdfs, thumbnails, crawling, requests...
- Celery needs a Message Queue
- Instead of RabbitMQ, try Redis.
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Redis:
"An advanced key-value store. It is often referred to as a data structure
server since keys can contain strings, hashes, lists, sets and sorted sets."

- store ephemeral data (active sessions)
- general cache (memcached compatible)
- real-time calculations: stats, monitoring, throttling...
- messages: channels (pub-sub), lists (push/blpop)
- indexes/filters (“sort by hits”)
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Sentry:
"realtime event logging and aggregation platform"
- monitor errors, get all the info to do a post-mortem
- uses the Python logger, easy to configure
- deploy a Sentry instance
- or use getsentry.com
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Debugging:
- ipython (already in ./manage.py shell)
- ipdb

import ipdb
ipdb.set_trace()

- django-debug-toolbar
- very powerful
- use it to optimize db performance, view by view
David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices
Summary:
- follow the Django philosophy (when possible)
- stand on the shoulder of giants: use existing apps

David Arcos | @DZPM

Catchoom | http://catchoom.com
12 tips on Django Best Practices

Thanks for attending!
- http://slideshare.net/DZPM
- Questions?

David Arcos | @DZPM

Catchoom | http://catchoom.com
Questions?

catchoom.com | @catchoom
12 tips on Django Best Practices

Thanks for attending!
- http://slideshare.net/DZPM

David Arcos | @DZPM

Catchoom | http://catchoom.com

More Related Content

PDF
Intro to Web Development Using Python and Django
PDF
Specification-By-Example with Gherkin
PDF
The Best (and Worst) of Django
PPTX
Sonatype nexus 로 docker registry 관리하기
KEY
Introduction Django
PDF
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
 
PDF
Compositionality and Category Theory - a montage of slides/transcript for sec...
ODT
Testing in-python-and-pytest-framework
Intro to Web Development Using Python and Django
Specification-By-Example with Gherkin
The Best (and Worst) of Django
Sonatype nexus 로 docker registry 관리하기
Introduction Django
Deep Dive: Alfresco Core Repository (... embedded in a micro-services style a...
 
Compositionality and Category Theory - a montage of slides/transcript for sec...
Testing in-python-and-pytest-framework

What's hot (20)

PDF
두 번째 startlink.live: 오현석 (algoshipda) - 초심자를 위한 알고리즘 공부 전략
PPTX
Capabilities for Resources and Effects
PDF
Django in Production
PPTX
React Native
PPTX
d.ts 만들기
PPTX
Gazebo, 9개의 파일로 간단히 시작하는 로봇 시뮬레이션
PPTX
Java 11 to 17 : What's new !?
PDF
Learn D3.js in 90 minutes
PPTX
Présentation de ECMAScript 6
PDF
공간데이터베이스(Spatial db)
PDF
Django congress jp 2019 make query great again! (slide share)
PDF
[NDC16] Effective Git
PDF
Introducing Kogito
PDF
New PHP Exploitation Techniques
PPTX
Gerrit Code Review with GitHub plugin
PDF
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
PDF
Kiss PageObjects [01-2017]
PDF
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
PDF
Why Vue.js?
PPTX
View Inheritance in Odoo 15
두 번째 startlink.live: 오현석 (algoshipda) - 초심자를 위한 알고리즘 공부 전략
Capabilities for Resources and Effects
Django in Production
React Native
d.ts 만들기
Gazebo, 9개의 파일로 간단히 시작하는 로봇 시뮬레이션
Java 11 to 17 : What's new !?
Learn D3.js in 90 minutes
Présentation de ECMAScript 6
공간데이터베이스(Spatial db)
Django congress jp 2019 make query great again! (slide share)
[NDC16] Effective Git
Introducing Kogito
New PHP Exploitation Techniques
Gerrit Code Review with GitHub plugin
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
Kiss PageObjects [01-2017]
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
Why Vue.js?
View Inheritance in Odoo 15
Ad

Similar to 12 tips on Django Best Practices (20)

DOCX
Django: Best Practices for Optimized Development and Deployment
PPTX
Django deployment best practices
PDF
Efficient Django
PDF
Django best practices
PDF
Building a custom cms with django
PDF
Django Designing.pdf
KEY
Intro To Django
PPTX
Journey through high performance django application
KEY
DjangoCon recap
PDF
Django at Scale
KEY
Django Deployment with Fabric
PDF
Django Overview
PDF
Scalable Django Architecture
PDF
Where Django Caching Bust at the Seams
PDF
Django
PDF
5 best practices for (web/ software) development (2010)
PDF
Django production
PDF
Towards Continuous Deployment with Django
PPTX
Prototype4Production Presented at FOSSASIA2015 at Singapore
PDF
Top 10 Django Tips for Beginners in 2025.pdf
Django: Best Practices for Optimized Development and Deployment
Django deployment best practices
Efficient Django
Django best practices
Building a custom cms with django
Django Designing.pdf
Intro To Django
Journey through high performance django application
DjangoCon recap
Django at Scale
Django Deployment with Fabric
Django Overview
Scalable Django Architecture
Where Django Caching Bust at the Seams
Django
5 best practices for (web/ software) development (2010)
Django production
Towards Continuous Deployment with Django
Prototype4Production Presented at FOSSASIA2015 at Singapore
Top 10 Django Tips for Beginners in 2025.pdf
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
cuic standard and advanced reporting.pdf
sap open course for s4hana steps from ECC to s4
Unlocking AI with Model Context Protocol (MCP)
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectroscopy.pptx food analysis technology

12 tips on Django Best Practices

  • 1. 12 tips on Django Best Practices David Arcos catchoom.com | @catchoom
  • 2. 12 tips on Django Best Practices Some (personal) suggestions on: - Development - Deployment - External tools David Arcos | @DZPM Catchoom | http://catchoom.com
  • 3. 12 tips on Django Best Practices Hi! I'm David Arcos - Python/Django developer - discovered Django in 2007 (v0.96) - professionally since 2008 - Web backend, distributed systems, databases, scalability, security - Team leader at Catchoom David Arcos | @DZPM Catchoom | http://catchoom.com
  • 4. 12 tips on Django Best Practices - Image Recognition SaaS - Top recognition results, shortest response times - Easy to integrate into your apps/services David Arcos | @DZPM Catchoom | http://catchoom.com
  • 5. 12 tips on Django Best Practices Why Python? PEP 20, The Zen of Python: - “Beautiful is better than ugly” - “Simple is better than complex” - “Complex is better than complicated” - “Readability counts” - “Special cases aren't special enough to break the rules” - “If the implementation is hard to explain, it's a bad idea” - (…) http://www.python.org/dev/peps/pep-0020/ David Arcos | @DZPM Catchoom | http://catchoom.com
  • 6. 12 tips on Django Best Practices Why Django? - "The Web framework for perfectionists with deadlines" - Django design philosophy: - loose coupling, less code, DRY, consistency, etc... - https://docs.djangoproject.com/en/dev/misc/design-philosophies/ - technically: tons of django apps, very good doc David Arcos | @DZPM Catchoom | http://catchoom.com
  • 7. 12 tips on Django Best Practices Virtualenv: - “one project, one virtualenv” - projects with different dependencies, package versions - easier to deploy. Forget dependency hell! - virtualenvwrapper is a convenient tool David Arcos | @DZPM Catchoom | http://catchoom.com
  • 8. 12 tips on Django Best Practices Dependencies: - use pip: pip install catchoom - save the dependencies in a requirements.txt file: pip freeze > requirements.txt pip install -r requirements.txt David Arcos | @DZPM Catchoom | http://catchoom.com
  • 9. 12 tips on Django Best Practices Layout: projects and apps - project = the full website. app = python library repository/ |-- doc `-- project |-- apps | |-- app1 | |-- app2 | `-- app3 `-- settings David Arcos | @DZPM Catchoom | http://catchoom.com
  • 10. 12 tips on Django Best Practices - use short, obvious, single-word names for your apps - many small apps is better than a few giant apps: - explain an app in a sentence. If you can't, split the app - rather than expand an app, write a new app - don't reinvent the wheel! - django.contrib - 3rd-party apps David Arcos | @DZPM Catchoom | http://catchoom.com
  • 11. 12 tips on Django Best Practices Settings: - multiple settings files: - per environment: dev, testing, staging, production - per developer (local settings, use the hostname) - all settings files must inherit from base, so you can do: INSTALLED_APPS += ('debug_toolbar', ) - version control all the settings! David Arcos | @DZPM Catchoom | http://catchoom.com
  • 12. 12 tips on Django Best Practices Django is a MTV framework - Model (app/models.py) - Template (app/templates/*.html) - View (app/views.py) David Arcos | @DZPM Catchoom | http://catchoom.com
  • 13. 12 tips on Django Best Practices Fat models, thin views... - logic should go to the models (and forms, signals...) - keep the views at a minimum Good example: django.contrib.auth.models.User David Arcos | @DZPM Catchoom | http://catchoom.com
  • 14. 12 tips on Django Best Practices - why? Because of maintainability! - a model is much easier to test - reusable logic: form validation, signals, etc - the code becomes clearer, more self-documenting David Arcos | @DZPM Catchoom | http://catchoom.com
  • 15. 12 tips on Django Best Practices ...and stupid templates! - your template layer should be as thin as possible - (by design) django templates are limited, constrained - doesn't fit your use case? Use jinja2 in those views - Hey, but I get ugly generated HTML! - doesn't matter, you want maintainable templates David Arcos | @DZPM Catchoom | http://catchoom.com
  • 16. 12 tips on Django Best Practices Deployment: - web server: - Nginx + gunicorn - Supervisord to keep it alive. - static server: - Nginx. Or any CDN. David Arcos | @DZPM Catchoom | http://catchoom.com
  • 17. 12 tips on Django Best Practices Fabric: "a library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks" - to automate deployments, migrations, execute management commands, monitoring... - no more repetitive maintainance tasks done manually! David Arcos | @DZPM Catchoom | http://catchoom.com
  • 18. 12 tips on Django Best Practices South: "intelligent schema and data migrations for Django projects" - creates migration files automatically. - You can still do changes - can do backward migrations - will avoid disasters. Use it! David Arcos | @DZPM Catchoom | http://catchoom.com
  • 19. 12 tips on Django Best Practices Celery: "asynchronous task queue/job queue based on distributed message passing" - execute tasks asynchronously, in a pool workers - cpu-intensive or I/O-intensive tasks: - emails, pdfs, thumbnails, crawling, requests... - Celery needs a Message Queue - Instead of RabbitMQ, try Redis. David Arcos | @DZPM Catchoom | http://catchoom.com
  • 20. 12 tips on Django Best Practices Redis: "An advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets." - store ephemeral data (active sessions) - general cache (memcached compatible) - real-time calculations: stats, monitoring, throttling... - messages: channels (pub-sub), lists (push/blpop) - indexes/filters (“sort by hits”) David Arcos | @DZPM Catchoom | http://catchoom.com
  • 21. 12 tips on Django Best Practices Sentry: "realtime event logging and aggregation platform" - monitor errors, get all the info to do a post-mortem - uses the Python logger, easy to configure - deploy a Sentry instance - or use getsentry.com David Arcos | @DZPM Catchoom | http://catchoom.com
  • 22. 12 tips on Django Best Practices Debugging: - ipython (already in ./manage.py shell) - ipdb import ipdb ipdb.set_trace() - django-debug-toolbar - very powerful - use it to optimize db performance, view by view David Arcos | @DZPM Catchoom | http://catchoom.com
  • 23. 12 tips on Django Best Practices Summary: - follow the Django philosophy (when possible) - stand on the shoulder of giants: use existing apps David Arcos | @DZPM Catchoom | http://catchoom.com
  • 24. 12 tips on Django Best Practices Thanks for attending! - http://slideshare.net/DZPM - Questions? David Arcos | @DZPM Catchoom | http://catchoom.com
  • 26. 12 tips on Django Best Practices Thanks for attending! - http://slideshare.net/DZPM David Arcos | @DZPM Catchoom | http://catchoom.com