SlideShare a Scribd company logo
Django Best Practices
Disclaimer
● This talk should take approximately 1 month
● I don't always take my own advice
● I'm not an expert
● I'm going to forget something
● You have a different opinion than me
A note on personal philosophy
● Organization makes chaos manageable
○ PEP8
○ Code standards
○ DRY
○ Hierarchical organization lets me understand what I
need to to solve the problem in front of me
A note on personal philosophy
● Provide value with as few LOC as possible
○ "Everything should be made as simple as possible,
but not simpler"
● "Unix Philosophy"
○ Read "The Art of Unix Programming"
○ Things should fail noisily
○ Don't surprise me
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
README
● Your project should have a README
○ Seriously
○ You want to go on vacation
● Format it in some way that's easy to read
○ duh
● What should probably be there
○ Installation instructions
○ Design decisions
○ Contribution information (or put this in a Contributing file)
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Requirements
● We use pip, with pretty results
● Pin your versions
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Continuous Integration
● Run your tests regularly
● We use circleci
○ Pretty simple
○ Integrates with github
○ still working out their own bugs
● Other options
○ Buildbot
○ Jenkins
○ TravisCI
○ Codeship.io
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Fabric
● Useful for deployment to remote machines
○ Supports parallel remote execution
● Nice python replacement for
○ Bash scripts
○ Makefiles
○ Duct Tape
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Things that shouldn't be in the
REPO
● Generated Files
● Data files
● .pyc files
● Backup files
● Cat pictures
○ Ok, maybe cat pictures
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
virtualenv
● Oh my god use it
● Keeps your projects from stepping on each
other
● World Peace
● Kittens
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Documentation
● IMHO lots of docs in the root make a project
hard to navigate
● Plaintext format that can generate html docs
preferred
○ ReStructured Text
○ Sphinx
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Project Configuration
● Non-sensitive configuration files
● Avoid putting these in root, again
hierarchical structure
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Newrelic
● Makes my life so much better
● Monitoring
○ Exceptions
○ Response Time / Profiling
○ Database profiling
● Alternatives
○ Sentry (FOSS)
○ App Dynamics
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Root project urls
● If at all possible should only include app's url
patterns
from django.conf.urls import patterns, url, include
urlpatterns = patterns(
'project',
url(r'achievements/', include('achievements.urls'))
)
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
WSGI
● How your webserver actually hooks into your
app
● WSGI middlewares can go here
○ Haven't ever found a user for them
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Settings
● Recently we've started using environment
variables with some success
● We've also used approaches where we have
a settings directory who's __init__.py file
determins appropriate settings for
environment
● Try to be organized, honestly, just use ENV
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Unpackaged dependencies
● Whenever possible use pip / setuptools
● Put django snippets / projects that aren't in
pypi in lib/
● If you're going to do something dirty try to be
organized about it
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Project specific Code
● As much as possible bundle code into
reusable apps
● Things that might have to go here
○ Custom test runners
○ Project middlewares
○ Project template context processors
○ Functions that don't make sense elsewhere
○ dbrouters
Project Structure
README.md
requirements.txt
circle.yml
fabfile.py
.gitignore
venv/
docs/
conf/
newrelic.ini
project_name/
__init__.py
urls.py
wsgi.py
settings/
lib/
base/
apps/
Django Applications
● Re-usuable apps make Django awesome
○ Use relative imports
○ Try to not include anything project specific
● Combine / split projects
○ Possible with proper organization :)
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Application Tests
● You should have tests
○ TDD/BDD if possible
○ IMHO your model tests / design give better ROI
○ If you find a bug write a test to reproduce, fix
○ Seriously testing will save you time
○ Really
○ Please
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Models
● Models are the lifeblood of your apps
○ Start with model design first
○ Design your data model well and views will come
naturally
● Model Managers are pretty awesome
○ Your views (controllers) should be able to call 1/2
model manager methods in order to get what it
needs otherwise code needs to be refactored into a
model manager
● Querysets can be subclassed as well
○ can make view / task code much much easier
○ DRY
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Application Urls
● All views accessible in an app should be
bundled into an app level urls file
● Use named url patterns to separate patterns
from controllers.
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Django Admin
● Major win
● Use django admin for as much as you can
● Most aspects are subclassable/extentable
● Your business guys don't need shiny UI
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Application Views
● Make the views as simple as possible
○ Template views can probably be functions that call
render
○ If a view has lots of model calls that code can
probably go into a manager
■ Manager are easier to test
■ If managers have lots of interdependencies your
data model probably needs to be refactored
● Use django shortcuts when possible
● IMHO template logic is ok to a point if it
simplifies view code, but don't go overboard
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Tasty Pie
● Don't re-invent the wheel
● Tastypie is useful for most (but not all)
RESTful applications
● The code is already tested
● You don't need as much control over JSON
structure as you think.
● This is a separate talk, but be aware of it.
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Background / Async in Django
● There are lots of choices for this
● ZMQ
○ Lots of options for routing
○ VERY flexible
○ VERY fast
○ VERY cool
○ Django z-tasks looks promising
○ BUT
■ Have to write your own workers
■ Have to write your own logging / monitoring
■ Have to understand a fair amount to make it work
correctly.
■ SO...
Celery
● Should be your default choice for async
● Very simple python API
○ Async tasks
○ Tasks in the future
○ Cron replacement
● Workers are already written
● Integrated with new relic
○ Ops have been much easier for us
○ Traceback monitoring major win
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
forms.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Django Forms
● Model Forms
○ DRY
○ Handles validation in a declarative way
○ Will make your life better
● Django crispy forms
○ More control
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
South
● Reproducible migrations
○ Schema migrations
○ Data migrations
○ Test runner
○ Probably going to be included in Django 1.6
○ Migration dependencies can be defined
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Management Commands
● We used to use these for cron jobs
○ Celery beat is nicer now
● Helpful for project wide scripts that require
django context
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Static Files
● Namespace your static files
○ app_name/static/app_name
○ avoids collisions
○ common static files can go in the base of the project
App Directory Structure
app_name/
tests.py
models.py
urls.py
admin.py
views.py
api.py
tasks.py
migrations/
management/
__init__.py
commands/
command_name.py
static/
app_name/
js/
css/
img/
templates/
app_name/
Application templates
● Same as static files, namespace them
● Use template inheritance
○ DRY
● Use named url patterns in your templates
● Don't go overboard with template logic
○ If you must check out template includes
○ Huge templates are hard to read
● Lots of view code data munging can be
replaced with template filters.
● Django templates are really controllers
○ Let templates act like views, handle presentation.
Other things you should be aware of
● Middlewares
● Class Based Views
● Template Context Processors
● Custom Template Tags
Django Apps that make me happy
● Django Extensions
○ Werkzeug
● South
○ Probably coming to Django 1.6
Tools you should use
● Code Quality
○ PEP8
○ Pyflakes
○ Pychecker
○ jshint
○ jslint
● Development Environment
○ ack-grep
○ emacs :)
● Infrastructure
○ PGbouncer
What did I forget?

More Related Content

PDF
12 tips on Django Best Practices
PDF
Django Best Practices
PDF
Best Practices for Front-End Django Developers
PDF
Django Documentation
PDF
Free django
PPT
Top 10 Scalability Mistakes
PDF
Unbreaking Your Django Application
PDF
Don't make me wait! or Building High-Performance Web Applications
12 tips on Django Best Practices
Django Best Practices
Best Practices for Front-End Django Developers
Django Documentation
Free django
Top 10 Scalability Mistakes
Unbreaking Your Django Application
Don't make me wait! or Building High-Performance Web Applications

What's hot (20)

PPT
Top 30 Scalability Mistakes
PPT
Top 10 Scalability Mistakes
PPT
Apache Con 2008 Top 10 Mistakes
PDF
RxNetty
PPTX
How does one learn to program?
PDF
Realtime Apps with Django
ODP
Django for Beginners
PPT
Ajax to the Moon
KEY
Web application development with Django framework
PDF
Validating big data pipelines - FOSDEM 2019
PPTX
Building Open-Source React Components
PPTX
Building Open-source React Components
ODP
Xdebug for Beginners
PDF
Spaghetti gate
PPTX
Untangling spring week8
PDF
TestIstanbul May 2013 Keynote Experiences With Exploratory Testing
PDF
Lessons Learned When Automating
PDF
If you want to automate, you learn to code
PPTX
Risk Mitigation Using Exploratory and Technical Testing - QASymphony Webinar ...
PPTX
Try harder or go home
Top 30 Scalability Mistakes
Top 10 Scalability Mistakes
Apache Con 2008 Top 10 Mistakes
RxNetty
How does one learn to program?
Realtime Apps with Django
Django for Beginners
Ajax to the Moon
Web application development with Django framework
Validating big data pipelines - FOSDEM 2019
Building Open-Source React Components
Building Open-source React Components
Xdebug for Beginners
Spaghetti gate
Untangling spring week8
TestIstanbul May 2013 Keynote Experiences With Exploratory Testing
Lessons Learned When Automating
If you want to automate, you learn to code
Risk Mitigation Using Exploratory and Technical Testing - QASymphony Webinar ...
Try harder or go home
Ad

Similar to Django best practices (20)

PDF
Django Overview
PDF
GDG Addis - An Introduction to Django and App Engine
PDF
Django for mobile applications
PDF
Reusable Apps
PDF
Building a custom cms with django
PPTX
ADF- Lect-3 Django Project Structure, Django Apps Structure.pptx
PDF
Django
KEY
Django Deployment with Fabric
PDF
Django Workflow and Architecture
PDF
Learn Django Tips, Tricks & Techniques for Developers
DOCX
Akash rajguru project report sem v
PDF
Web development django.pdf
PDF
Django dev-env-my-way
PDF
Django Introduction & Tutorial
PPTX
Introduction to django
PDF
Towards Continuous Deployment with Django
PDF
Django - basics
ODP
Introduce Django
KEY
Intro To Django
Django Overview
GDG Addis - An Introduction to Django and App Engine
Django for mobile applications
Reusable Apps
Building a custom cms with django
ADF- Lect-3 Django Project Structure, Django Apps Structure.pptx
Django
Django Deployment with Fabric
Django Workflow and Architecture
Learn Django Tips, Tricks & Techniques for Developers
Akash rajguru project report sem v
Web development django.pdf
Django dev-env-my-way
Django Introduction & Tutorial
Introduction to django
Towards Continuous Deployment with Django
Django - basics
Introduce Django
Intro To Django
Ad

Django best practices