The Django Web Framework (EuroPython 2006)

Post on 06-May-2015

2.113 views 5 download

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)

The Django Web Framework

Simon Willisonhttp://simonwillison.net/

EuroPython, 3rd July 2006

Web development on Journalism deadlines

... in three days

CharacteristicsClean URLs

Loosely coupled components

Designer-friendly templates

Less code

Really fast development

Components

URL dispatching

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

What code shall we execute?

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

Views

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

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

Models

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

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

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

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

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

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;

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

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

>>> p.id1

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

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

>>> 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?"]

Templates

Gluing strings together gets old fast

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

<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 %}

<h1>Hello World!</h1>

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

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

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

Common headers and footers?

Template inheritance

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>

home.html

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

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

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

Loosely coupled

Icing

BengaliCzechWelshDanishGermanGreekEnglishSpanishFrenchGalicianHebrewIcelandicItalian

JapaneseDutch

NorwegianBrazilian

RomanianRussianSlovak

SlovenianSerbianSwedish

UkrainianSimplified ChineseTraditional Chinese

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

Forms are boring

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!

Model validation rules + the Manipulator API

do all of this for you

The admin package does even more

No time to talk about...

Generic views

Data object serialisation

Syndication framework

Middleware

Authentication and authorisation

Success stories

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

Find out more