2. 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
3. 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
4. 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
7. 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)
11. 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
19. 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
23. Newrelic
● Makes my life so much better
● Monitoring
○ Exceptions
○ Response Time / Profiling
○ Database profiling
● Alternatives
○ Sentry (FOSS)
○ App Dynamics
25. 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'))
)
29. 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
31. 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
33. 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
35. 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 :)
37. 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
39. 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
41. 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.
43. 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
45. 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
47. 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.
49. 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...
50. 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
54. South
● Reproducible migrations
○ Schema migrations
○ Data migrations
○ Test runner
○ Probably going to be included in Django 1.6
○ Migration dependencies can be defined
60. 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.
61. Other things you should be aware of
● Middlewares
● Class Based Views
● Template Context Processors
● Custom Template Tags
62. Django Apps that make me happy
● Django Extensions
○ Werkzeug
● South
○ Probably coming to Django 1.6
63. Tools you should use
● Code Quality
○ PEP8
○ Pyflakes
○ Pychecker
○ jshint
○ jslint
● Development Environment
○ ack-grep
○ emacs :)
● Infrastructure
○ PGbouncer