Django: The Good Parts

James Bennett (Release Manager for Django)
  • July 2005: Initial public preview
  • Now: 160 kLOC of Python in Django 1.7

Aren't other frameworks smaller/lighter and thus better?

The Good Parts

  • HTTP abstraction + request/response cycle
  • Conservative (really!) approach to features
  • The secret weapons: apps

HTTP Abstraction

HTTP sucks

  • Nine different request methods with big differences
  • Unicode-unaware
  • Persistence, auth ,streaming, security al tacked on/afterthoughts

CGI isn't great, either

  • How to CGI
    • Write script to be invoked by web server; request information will be in environment variables
    • submitted data received via standar input
    • script does it processing
  • How CGI is actually done
    • Script maybe invoked by web server
    • chmod 777 everything
    • environment is full of LIES
    • give up figuring out what the actual request URL was

WSGI is basically CGI with a bit of Python

  • Having a standard is good, but...
    • It's CGI-style; you to constantly reparse environment variables
    • signaling up and down the stack requires inventing ad-hoc protocols
    • return/response API sucks
    • inherits HTTP's awful approach to character encoding

We can do better

  • HttpRequest
    • already parsed and normalize for you
    • sensible attribute and dictionary-based access
    • relatively san handling of character encoding
  • HttpResponse
    • easy to construct and manipulate
    • convenient sublcasses for common specialed responses (404, redirect, etc.)
  • Write a callable that takes a HttpRequest and returns an HttpResponse
  • Write simple URL routing

Life cycle

  • Middleware system baked in... all middleware systems suck, though
  • Signals!
  • Decorators!
  • Call other stuff - it's callables all the way down

Django does PEP 333 so you don't have to

  • A Django project is a WSGI application
  • wsgi.py + WSGI_APPLICATION

Sane HTTP/WSGI is worth importing some stuff

Compassionate Conservatism

Features take a while to get in

  • The truncatechars function only took 4 years!
  • Migrations only took 6 years!

Unbundling/removing

  • contrib.comments
  • contrib.databrowse
  • contrib.localflavor
  • contrib.markup

Things are difficult to add, easier to remove. This is usually described as a bad thing.

Conservatism

  • strong preference or community solutions (e.g., Migrations: South won the migration wars)
  • core framework more about providing API support
  • more "swappable" components than expected (e.g., Jinja2 instead of the template system)
  • more stability over time
  • features land when ready
  • competition often produces better solutions
  • Django stays remarkable consistent and stable... this is good!

Enc-app-sulation

  • A Django project is a WSGI application. But what is a Django application?

Django Applications

  • Django apps can be...
    • models
    • views
    • forms
    • etc.
    • Not generally a black box
  • Django gives you safe APIs, meaning you can make safe assumptions and safer integration
  • Django applications are encapsulated, pluggable functionality
    • Key selling point of Django
    • Apps are a secret weapon
  • A Django application can do one thing and do it well
  • Django Packages lists 2,206 available apps