The Django Web Framework (EuroPython 2006)

56
The Django Web Framework Simon Willison http://simonwillison.net/ EuroPython, 3rd July 2006

description

I just found this old presentation, and thought it was interesting as something I put together only a year after Django's first release.

Transcript of The Django Web Framework (EuroPython 2006)

Page 1: The Django Web Framework (EuroPython 2006)

The Django Web Framework

Simon Willisonhttp://simonwillison.net/

EuroPython, 3rd July 2006

Page 2: The Django Web Framework (EuroPython 2006)

Web development on Journalism deadlines

Page 3: The Django Web Framework (EuroPython 2006)
Page 4: The Django Web Framework (EuroPython 2006)
Page 5: The Django Web Framework (EuroPython 2006)
Page 6: The Django Web Framework (EuroPython 2006)
Page 7: The Django Web Framework (EuroPython 2006)

... in three days

Page 8: The Django Web Framework (EuroPython 2006)

CharacteristicsClean URLs

Loosely coupled components

Designer-friendly templates

Less code

Really fast development

Page 9: The Django Web Framework (EuroPython 2006)

Components

Page 10: The Django Web Framework (EuroPython 2006)

URL dispatching

Page 11: The Django Web Framework (EuroPython 2006)

http://www.example.com/poll/5/

What code shall we execute?

Page 12: The Django Web Framework (EuroPython 2006)

(r'^$', 'views.index'), (r'^hello/$', ‘views.hello'), (r'^poll/(\d+)/$', ‘views.poll'),

Page 13: The Django Web Framework (EuroPython 2006)

Views

Page 14: The Django Web Framework (EuroPython 2006)

def index(request): s = "Hello, World" return HttpResponse(s)

Page 15: The Django Web Framework (EuroPython 2006)

def index(request): name = request.GET['name'] s = "Hi, " + escape(name) return HttpResponse(s)

Page 16: The Django Web Framework (EuroPython 2006)

Models

Page 17: The Django Web Framework (EuroPython 2006)

...(r'^poll/(\d+)/$', 'views.poll'),...

Page 18: The Django Web Framework (EuroPython 2006)

...(r'^poll/(\d+)/$', 'views.poll'),...

def poll(request, poll_id): poll = Poll.objects.get( pk=poll_id ) return HttpResponse( 'Question: ' + poll.question )

Page 19: The Django Web Framework (EuroPython 2006)

class Poll(Model): question = CharField(maxlength=200) pub_date = DateTimeField()

class Choice(Model): poll = ForeignKey(Poll) choice = CharField(maxlength=200) votes = IntegerField()

Page 20: The Django Web Framework (EuroPython 2006)

BEGIN;CREATE TABLE "polls_poll" ( "id" serial NOT NULL PRIMARY KEY, "question" varchar(200) NOT NULL, "pub_date" timestamp with time zone NOT NULL);CREATE TABLE "polls_choice" ( "id" serial NOT NULL PRIMARY KEY, "poll_id" integer NOT NULL REFERENCES "polls_polls" ("id"), "choice" varchar(200) NOT NULL, "votes" integer NOT NULL);COMMIT;

Page 21: The Django Web Framework (EuroPython 2006)

p = Poll( question = "What's up?", pub_date = datetime.now())p.save()

p.choice_set.create( choice = "Some choice", votes = 0)

Page 22: The Django Web Framework (EuroPython 2006)

>>> p.id1

>>> p.question"What's up?"

>>> p.choice_set.all()[<Choice: Some choice>]

Page 23: The Django Web Framework (EuroPython 2006)

>>> Poll.objects.count()7

>>> Poll.objects.filter( pub_date__year = 2006, pub_date__month = 5)["What's up?"]

>>> Poll.objects.all()[0:2]["What's up", "Like Django?"]

Page 24: The Django Web Framework (EuroPython 2006)

Templates

Page 25: The Django Web Framework (EuroPython 2006)

Gluing strings together gets old fast

Page 26: The Django Web Framework (EuroPython 2006)

c = Context({ 'today': datetime.date.today(), 'edibles': ['pear', 'apple', 'orange']})

Page 27: The Django Web Framework (EuroPython 2006)

<h1>Hello World!</h1>

<p>Today is {{ today|date:"jS F, Y" }}</p>

{% if edibles %}<ul> {% for fruit in edibles %} <li>{{ fruit }}</li> {% endfor %}</ul>{% endif %}

Page 28: The Django Web Framework (EuroPython 2006)

<h1>Hello World!</h1>

<p>Today is 2nd July, 2006</p>

<ul> <li>pear</li> <li>apple</li> <li>orange</li></ul>

Page 29: The Django Web Framework (EuroPython 2006)

def hello(request): return render_to_response('hello.html',{ 'today': datetime.date.today(), 'edibles': ['pear', 'apple', 'orange'] })

Page 30: The Django Web Framework (EuroPython 2006)

Common headers and footers?

Page 31: The Django Web Framework (EuroPython 2006)

Template inheritance

Page 32: The Django Web Framework (EuroPython 2006)

base.html<html><head><title> {% block title %}{% endblock %}</title></head><body>{% block main %}{% endblock %}<div id=”footer”>{% block footer %}(c) 2006{% endblock %}</div></body></html>

Page 33: The Django Web Framework (EuroPython 2006)

home.html

{% extends “base.html” %}{% block title %}Homepage{% endblock %}

{% block main %}Main page contents goes here.{% endblock %}

Page 34: The Django Web Framework (EuroPython 2006)

combined<html><head><title> Homepage</title></head><body>Main page contents goes here.<div id=”footer”>(c) 2006</div></body></html>

Page 35: The Django Web Framework (EuroPython 2006)

Loosely coupled

Page 36: The Django Web Framework (EuroPython 2006)

Icing

Page 37: The Django Web Framework (EuroPython 2006)
Page 38: The Django Web Framework (EuroPython 2006)

BengaliCzechWelshDanishGermanGreekEnglishSpanishFrenchGalicianHebrewIcelandicItalian

JapaneseDutch

NorwegianBrazilian

RomanianRussianSlovak

SlovenianSerbianSwedish

UkrainianSimplified ChineseTraditional Chinese

Page 39: The Django Web Framework (EuroPython 2006)

msgid "Usernames cannot contain the '@' character."msgstr "Ім'я не може містити '@' символ"

Page 40: The Django Web Framework (EuroPython 2006)

Forms are boring

Page 41: The Django Web Framework (EuroPython 2006)

1. Display form

2. Validate submitted data

3. If errors, redisplay with:

3.1. Contextual error messages

3.2. Correct fields pre-filled

4. ... do something useful!

Page 42: The Django Web Framework (EuroPython 2006)

Model validation rules + the Manipulator API

do all of this for you

Page 43: The Django Web Framework (EuroPython 2006)

The admin package does even more

Page 44: The Django Web Framework (EuroPython 2006)
Page 45: The Django Web Framework (EuroPython 2006)

No time to talk about...

Generic views

Data object serialisation

Syndication framework

Middleware

Authentication and authorisation

Page 46: The Django Web Framework (EuroPython 2006)

Success stories

Page 47: The Django Web Framework (EuroPython 2006)
Page 48: The Django Web Framework (EuroPython 2006)
Page 49: The Django Web Framework (EuroPython 2006)
Page 50: The Django Web Framework (EuroPython 2006)
Page 51: The Django Web Framework (EuroPython 2006)
Page 52: The Django Web Framework (EuroPython 2006)

170+ public Django-powered sites

http://code.djangoproject.com/wiki/DjangoPoweredSites

90+ contributors

1900+ mailing list subscribers (and 900+ on the development list)

And the Journal-World have convinced three new people to go and work in Kansas

Page 53: The Django Web Framework (EuroPython 2006)

Find out more

Page 54: The Django Web Framework (EuroPython 2006)
Page 55: The Django Web Framework (EuroPython 2006)