• Like
  • Save
The Django Web Application Framework
Upcoming SlideShare
Loading in...5
×
 

The Django Web Application Framework

on

  • 39,699 views

Slides from a talk given to the ACCU Python Track in Oxford, on the 20th April 2006.

Slides from a talk given to the ACCU Python Track in Oxford, on the 20th April 2006.

Statistics

Views

Total Views
39,699
Views on SlideShare
38,278
Embed Views
1,421

Actions

Likes
43
Downloads
1,054
Comments
4

28 Embeds 1,421

http://django.vn 405
http://www.squidoo.com 326
http://www.lonerunners.net 171
http://www.lifeyun.com 169
http://0.0.0.0 95
http://www.slideshare.net 80
http://0ls.ru 58
http://www.tanasi.it 24
http://salty-oasis-9120.herokuapp.com 22
http://infiernogeek.blogspot.com 19
http://localhost 19
http://gpessia.wordpress.com 7
http://www.disordine.com 4
http://192.168.0.13 3
http://www.bibigeek.com 2
http://iyambae-on-net.blogspot.com 2
http://www.filescon.com 2
http://marketingvietnam.vn 2
http://pyautoservicio.blogspot.com 2
http://hedgehoglab.com 1
http://webcache.googleusercontent.com 1
http://openfrog.net 1
http://www.django.vn 1
http://www.python.rk.edu.pl 1
http://64.233.179.104 1
http://www.rkblog.rk.edu.pl 1
http://localhost:8000 1
http://www.rapidsharefast.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

14 of 4 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The Django Web Application Framework The Django Web Application Framework Presentation Transcript

    • The Django Web Application Framework Simon Willison http://simonwillison.net/ ACCU, Python Track, 20th April 2006
    • This talk An overview of Django What's in a web framework, anyway? Framework comparison (and some pretty screenshots, too)
    • Origins
    • Lawrence, Kansas - 2003
    • Web development on Journalism deadlines
    • ... in three days
    • The ideal framework... Clean URLs Loosely coupled components Designer-friendly templates As little code as possible Really fast development
    • We didn’t mean to build a framework...
    • ... but nothing else really fitted the bill
    • Building a framework
    • HTTP handling
    • GET /community/ HTTP/1.0 Host: www.djangoproject.com HTTP/1.1 200 OK Date: Wed, 19 Apr 2006 23:53:29 GMT Server: Apache Vary: Cookie Connection: close Content-Type: text/html; charset=utf-8 <!DOCTYPE html PUBLIC quot;-//W3C//DTD XHTML 1.0 Transitional//ENquot; quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtdquot;> <html xmlns=quot;http://www.w3.org/1999/xhtmlquot; xml:lang=quot;enquot; lang=quot;enquot;> <head> [...]
    • Parse the request; generate a response
    • GET variables, POST variables, cookies, uploaded files, caching, content negotiation
    • HttpRequest HttpResponse
    • def index(request): s = “Hello, World” return HttpResponse(s)
    • def index(request): r = HttpResponse( mimetype='text/plain' ) r.write(“Hello, World”) return r
    • def index(request): if request.GET: s = “Hi, %s” % escape(request.GET.get( ’name’, ‘anon’) else: s = “Hello, World” return HttpResponse(s)
    • URL dispatching
    • http://www.example.com/poll/5/ What code shall we execute?
    • urlpatterns = patterns('', (r'^$', 'views.index'), (r'^hello/', ‘views.hello'), (r'^poll/(d+)/', ‘views.poll'), )
    • Database access
    • ... (r'^poll/(d+)/', 'views.poll'), ... def poll(request, poll_id): poll = Poll.objects.get( pk=poll_id ) return HttpResponse( 'Title: ' + poll.title )
    • class Poll(Model): question = CharField(maxlength=200) pub_date = DateTimeField('date published') class Choice(Model): poll = ForeignKey(Poll) choice = CharField(maxlength=200) votes = IntegerField()
    • BEGIN; CREATE TABLE quot;polls_pollquot; ( quot;idquot; serial NOT NULL PRIMARY KEY, quot;questionquot; varchar(200) NOT NULL, quot;pub_datequot; timestamp with time zone NOT NULL ); CREATE TABLE quot;polls_choicequot; ( quot;idquot; serial NOT NULL PRIMARY KEY, quot;poll_idquot; integer NOT NULL REFERENCES quot;polls_pollsquot; (quot;idquot;), quot;choicequot; varchar(200) NOT NULL, quot;votesquot; integer NOT NULL ); COMMIT;
    • p = Poll( question = quot;What's up?quot;, pub_date = datetime.now() ) p.save()
    • >>> p.id 1 >>> p.question quot;What's up?quot; >>> p.pub_date datetime(2005, 7, 15, 12, 00, 53)
    • Templating
    • Gluing strings together gets old fast
    • { '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 20th April, 2006</p> <ul> <li>pear</li> <li>apple</li> <li>orange</li> </ul>
    • def hello(request): t = get_template('hello.html') c = Context({ 'today': datetime.date.today(), 'edibles': ['pear', 'apple', 'orange'] }) return HttpResponse(t.render(c))
    • All you really need are variables, conditionals and loops
    • Essential ingredients HTTP handling URL dispatching Templating Documentation Database access (optional) ... no wonder there are so many frameworks!
    • Extras
    • 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
    • django.contrib.admin does even more
    • class Poll(Model): question = CharField(maxlength=200) pub_date = DateTimeField('date published') class Admin: list_display = ('question', 'pub_date') class Choice(Model): poll = ForeignKey(Poll) choice = CharField(maxlength=200) votes = IntegerField() class Admin: pass
    • Smarter templating
    • 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>
    • Custom template tags
    • {{ page.content|textile }}
    • {{ page.content|textile }} {% comment_form for news.stories story.id with is_public yes photos_optional thumbs,200,400 ratings_optional scale:1-5|first_option|second_option %}
    • from django import template register = template.Library() def textile(value): try: import textile except ImportError: return value else: return textile.textile(value) register.filter(textile)
    • i18n and l10n
    • Bengali Japanese Czech Dutch Welsh Norwegian Danish Brazilian German Romanian Greek Russian English Slovak Spanish Slovenian French Serbian Galician Swedish Hebrew Ukrainian Icelandic Simplified Chinese Italian Traditional Chinese
    • Authentication and authorisation
    • Community
    • ?
    • It’s a matter of taste
    • HTTP handling What happens to form variables? GET vs POST How to handle /path?foo=1&foo=2 How to send back non-standard responses Different Content-Type (and other) headers 404s, 500s Session support?
    • URL dispatching Object traversal or explicit configuration? Reversible URLs? Parameter extraction Trailing slashes?
    • Database handling To ORM or not to ORM? Pluralisation? Handling joins Lookup syntax When should you fall back on raw SQL?
    • Templating Plain text or XML? Markup visible on the page or hidden in HTML attributes? Logic in the template vs logic in the view/ controller Safe (and limited) or unsafe (and more powerful) Language extension mechanism?
    • Where’s the line? Authentication and authorisation? Automated admin / forms? i18n and l10n? JavaScript and Ajax?
    • Getting involved
    • Summer of Code 2006
    • www.djangoproject.com
    • Extra slides
    • Comparisons
    • $_GET, $_POST, HTTP $_REQUEST etc URL dispatch Filesystem Database handling mysql_* functions Templating <?php ... ?>
    • HTTP CherryPy Object path URL dispatch traversal Database handling SQLObject Kid (XML), Templating common templating API
    • HTTP ActionController? Object traversal, URL dispatch Routes Database handling ActiveRecord Templating ERb, Builder, RJS