Your SlideShare is downloading. ×
0
The web framework for perfectionists with deadlines



James Casey
2nd October 2009
What’s Django?
“Django is a high-level Python Web framework that
encourages rapid development and clean, pragmatic
design....
Whence Django ?
‣ Internal project of newspaper in 2003
  ‣ Lawrence Journal-World
‣ Should help journalist meet faster de...
Django in the news

     ‣ http://mps-expenses.guardian.co.uk/
     ‣ MP expense scandal
        ‣ crowdsourcing the revie...
Django won a pulitzer

     ‣ http://polifact.com/
        ‣ Fact checking in 2008 US presidental
          election
     ...
Overview
Principles
‣ DRY (Don’t Repeat Yourself)
‣ Write less code
‣ Make CRUD easy
‣ DB neutral
 ‣ Oracle, MySQL, PostgreSQL, SQL...
It’s (just) python
Features
‣ Object-relational mapping (ORM)
‣ Automatic admin interface
‣ Elegant URL design
‣ Template system
‣ Caching
‣ ...
Architecture

             Browser


  Template             URL


              Views


             Models


         Dat...
Model-Template-View

‣ Models : What things are

‣ Views : How things are processed

‣ Templates : How things are presented
Models
from django.db import models

class Author(models.Model):
   name = models.CharField(max_length=100)
   age = model...
Represents the
          database objects
BEGIN;
CREATE TABLE `tutorial_author` (
    `id` integer AUTO_INCREMENT NOT NULL...
ORM
books = Book.objects.all()

books_this_year = Book.objects.filter(pubdate__gt = jan_1st)

apress_books = Book.objects....
Views
‣ Where all the magic happens
‣ Normally :
 ‣ Process model instances
 ‣ Render HTML
‣ Keep your logic in the model !
import datetime

def view_latest_books(request):
    # Last 5 days
    date = datetime.datetime.now() -
                  ...
Templates

‣ Separate design from code

‣ Separate designers from code

‣ Separate design from developers
“base.html”



<html>
  <head>
    <title>{% block title %}{% endblock %}</title>
  </head>
  <body>
    {% block content ...
“index.html”

{% extends "tutorial/base.html" %}
{% block title %}Homepage{% endblock %}
{% block content %}
  {% for book...
Security advantages
‣ No raw SQL from the users
 ‣ We deal with models and queries
‣ Automatic HTML escaping
 ‣ No XSS att...
This cannot happen !
URLs

urlpatterns = patterns('apps.tutorial.views',
    (r'^$', 'index'),

    (r’^book/<?P<id>d+)/$’, ‘show_book’),
    (...
http://example.com/tutorial/

http://example/com/tutorial/books/

http://example.com/tutorial/create/

http://example.com/...
Views are just python
          functions
import datetime

def view_latest_books(request, num_days):
    date = datetime.d...
Aggregation
      ‣ When you need to summarise a collection
        of objects
         ‣ Leveraging the DB where possible...
Other features
‣ Forms
‣ Generic Views
 ‣ Makes CRUD simple
‣ User management
‣ i18n
My first django project
Projects contain Applications
                       Project


                  my
                                  myap...
Getting started
‣ Install Django 1.1
 > easy_install django

‣ Create a project
 > django-admin.py startproject new_django...
> ./manage.py startapp tutorial


 new_django_project/
     __init__.py
     manage.py
     settings.py
     tutorial/
   ...
> ./manage.py runserver
Validating models...
0 errors found

Django version 1.1, using settings 'new_django_project.settin...
automatic admin interface


   http://localhost:8000/admin/tutorial/
manage.py
‣ syncdb : create SQL for your models
‣ shell : start up a python shell with your
  django project loaded
‣ test...
Tools to make you
 more productive
   all just an easy_install away
distutils
‣ ‘standard’ python packaging
  ‣ ./setup.py sdist : source packages
  ‣ /.setup.py bdist : binary packages
‣ Ni...
Virtualenv
‣ Run separate python environments
 ‣ With different sets of packages
 ‣ And even different interpreters
‣ Easi...
ipython
‣ python with CLI hotness
 ‣ TAB autocomplete
 ‣ code coloring
 ‣ nicer pdb integration
 ‣ ...


         http://i...
PIP
‣ Better installation manager
  ‣ ‘easy_install’ with dependency ordering
  ‣ Integrated with virtualenv
  ‣ Allow to ...
django-debug-toolbar


  http://localhost:8000/tutorial/
django-command-extensions

 ‣ Extra manage.py commands
  ‣ shell_plus : a better python shell
  ‣ runserver_plus : a bette...
Werkzeug


http://localhost:8000/tutorial/latest/5
and of course, unittest
‣ Django supports :
 ‣ doctest : useful for simple model validation
 ‣ unittest : you did write so...
reusable apps
if it’s useful, it’s probably been done before...
south

‣ Schema migration
 ‣ Change models over time
 ‣ Write upgrade routines
   ‣ just python functions with access
    ...
A platform for rapidly developing (social) websites
code.google.com

‣ just search for django-<WHATEVER> :)
‣ It’s probably there...
  ‣ If it’s not there, write it and put i...
Future
beyond django 1.1
Multi-DB

‣ Allows your models to be in multiple DBs
 ‣ Different applications in different DBs
 ‣ ‘sharding’ of objects a...
Other
‣ Admin UI enhancements
 ‣ autocompletion
 ‣ better inline handling
‣ Non-relational database support
   ‣ CouchDB, ...
want more ?
‣ http://docs.djangoproject.com/
‣ Django community RSS feed
  ‣ http://www.djangoproject.com/community/
‣ Mailing lists
 ...
Books
django-users@cern.ch
Thank you
Credits
‣ XKCD for cartoons
‣ Amazon.com for book pictures
‣ Initial inspiration for slides and examples
  from Joaquim Ro...
Introduction to Django
Upcoming SlideShare
Loading in...5
×

Introduction to Django

5,181

Published on

A set of slides introducting Django at CERNs White Areas lecture series.

Published in: Technology, Spiritual

Transcript of "Introduction to Django"

  1. 1. The web framework for perfectionists with deadlines James Casey 2nd October 2009
  2. 2. What’s Django? “Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.” from http://djangoproject.org/
  3. 3. Whence Django ? ‣ Internal project of newspaper in 2003 ‣ Lawrence Journal-World ‣ Should help journalist meet faster deadlines ‣ Should not stand in the way of journalists ‣ Named after the famous guitarist Django Reinhardt
  4. 4. Django in the news ‣ http://mps-expenses.guardian.co.uk/ ‣ MP expense scandal ‣ crowdsourcing the review of 500K documents ‣ 7 days from proof-of-concept to launch http://simonwillison.net/2009/talks/europython-crowdsourcing/
  5. 5. Django won a pulitzer ‣ http://polifact.com/ ‣ Fact checking in 2008 US presidental election ‣ Lead developer was former journalist ‣ It was his first django application http://www.mattwaite.com/posts/2007/aug/22/announcing-politifact/
  6. 6. Overview
  7. 7. Principles ‣ DRY (Don’t Repeat Yourself) ‣ Write less code ‣ Make CRUD easy ‣ DB neutral ‣ Oracle, MySQL, PostgreSQL, SQLlite ‣ Deployment platform neutral ‣ mod_python, WSGI, ...
  8. 8. It’s (just) python
  9. 9. Features ‣ Object-relational mapping (ORM) ‣ Automatic admin interface ‣ Elegant URL design ‣ Template system ‣ Caching ‣ i18n
  10. 10. Architecture Browser Template URL Views Models Database
  11. 11. Model-Template-View ‣ Models : What things are ‣ Views : How things are processed ‣ Templates : How things are presented
  12. 12. Models from django.db import models class Author(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() friends = models.ManyToManyField('self', blank=True) class Publisher(models.Model): name = models.CharField(max_length=300) num_awards = models.IntegerField() class Book(models.Model): isbn = models.CharField(max_length=9) name = models.CharField(max_length=300) pages = models.IntegerField() price = models.DecimalField(max_digits=10, decimal_places=2) rating = models.FloatField() authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) pubdate = models.DateField()
  13. 13. Represents the database objects BEGIN; CREATE TABLE `tutorial_author` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL, `age` integer NOT NULL ); CREATE TABLE `tutorial_publisher` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(300) NOT NULL, `num_awards` integer NOT NULL ); ... ... COMMIT;
  14. 14. ORM books = Book.objects.all() books_this_year = Book.objects.filter(pubdate__gt = jan_1st) apress_books = Book.objects.filter(publisher__name = ‘Apress’) ‣ Never write SQL again ‣ Unless you really need to
  15. 15. Views ‣ Where all the magic happens ‣ Normally : ‣ Process model instances ‣ Render HTML ‣ Keep your logic in the model !
  16. 16. import datetime def view_latest_books(request): # Last 5 days date = datetime.datetime.now() - datetime.timedelta(5) books = Book.objects.filter(pubdate__gte = date).order_by('-pubdate') return render_to_response('tutorial/show_books.html', {'books': books})
  17. 17. Templates ‣ Separate design from code ‣ Separate designers from code ‣ Separate design from developers
  18. 18. “base.html” <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> {% block content %}{% endblock %} </body> </html>
  19. 19. “index.html” {% extends "tutorial/base.html" %} {% block title %}Homepage{% endblock %} {% block content %} {% for book in books %} <h4>{{ book.name }}</h4> <p>Publisher: {{ book.publisher }}</p> <p>Date of Publication: {{ book.pubdate|date }}</p> <p>Price ${{ book.price }}</p> <p>Author : {% for a in book.authors.all %}{{ a.name }}{% if not forloop.last %}, {% endif %}{% endfor %}</p> {% endfor %} {% endblock %}
  20. 20. Security advantages ‣ No raw SQL from the users ‣ We deal with models and queries ‣ Automatic HTML escaping ‣ No XSS attacks ‣ CSRF protection ‣ No replay of forms by other code
  21. 21. This cannot happen !
  22. 22. URLs urlpatterns = patterns('apps.tutorial.views', (r'^$', 'index'), (r’^book/<?P<id>d+)/$’, ‘show_book’), (r'^latest/(?P<num_days>d+)/$', 'view_latest_books'), (r'^create/$', 'create'), ) ‣ URLs map to views (via regular expressions)
  23. 23. http://example.com/tutorial/ http://example/com/tutorial/books/ http://example.com/tutorial/create/ http://example.com/tutorial/latest/5/
  24. 24. Views are just python functions import datetime def view_latest_books(request, num_days): date = datetime.datetime.now() - datetime.timedelta(int(num_days)) books = Book.objects.filter(pubdate__gte = date).order_by('-pubdate') return render_to_response('books/show_books.html', {'books': books})
  25. 25. Aggregation ‣ When you need to summarise a collection of objects ‣ Leveraging the DB where possible > q = Book.objects.annotate(num_authors=Count('authors')) > [b.num_authors for b in q] [2, 3, 1] > Store.objects.aggregate(min_price=Min('books__price'), max_price=Max('books__price')) {‘min_price’ : 2.99, ‘max_price’ : 29.99} http://docs.djangoproject.com/en/dev/topics/db/aggregation/
  26. 26. Other features ‣ Forms ‣ Generic Views ‣ Makes CRUD simple ‣ User management ‣ i18n
  27. 27. My first django project
  28. 28. Projects contain Applications Project my myapp other_app reuseable app ‣ Application : self-contained set of functions ‣ Project : collection of applications, installed into same database ‣ roughly project == a web application - has a settings file
  29. 29. Getting started ‣ Install Django 1.1 > easy_install django ‣ Create a project > django-admin.py startproject new_django_project new_django_project/ __init__.py manage.py settings.py urls.py
  30. 30. > ./manage.py startapp tutorial new_django_project/ __init__.py manage.py settings.py tutorial/ __init__.py models.py tests.py views.py urls.py
  31. 31. > ./manage.py runserver Validating models... 0 errors found Django version 1.1, using settings 'new_django_project.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ... ‣ Now you write your code ... > ./manage.py syncdb Creating table django_content_type Creating table django_session Creating table django_site Creating table tutorial_author Creating table tutorial_publisher Creating table tutorial_book Creating table tutorial_store Installing index for tutorial.Book model
  32. 32. automatic admin interface http://localhost:8000/admin/tutorial/
  33. 33. manage.py ‣ syncdb : create SQL for your models ‣ shell : start up a python shell with your django project loaded ‣ test : run your unit tests ‣ you did write some, didn’t you ? ‣ inspectdb : reverse engineer models for existing DB ‣ loaddata / dumpdata : load/dump your fixtures from a DB
  34. 34. Tools to make you more productive all just an easy_install away
  35. 35. distutils ‣ ‘standard’ python packaging ‣ ./setup.py sdist : source packages ‣ /.setup.py bdist : binary packages ‣ Nice to integrate with other tools ‣ pip, unittest, ... ‣ ./setup.py bdist_rpm : Can produce rpm
  36. 36. Virtualenv ‣ Run separate python environments ‣ With different sets of packages ‣ And even different interpreters ‣ Easily switch between then ‣ virtualenv_wrapper gives nice bash functions for it all http://pypi.python.org/pypi/virtualenv
  37. 37. ipython ‣ python with CLI hotness ‣ TAB autocomplete ‣ code coloring ‣ nicer pdb integration ‣ ... http://ipython.scipy.org/moin/
  38. 38. PIP ‣ Better installation manager ‣ ‘easy_install’ with dependency ordering ‣ Integrated with virtualenv ‣ Allow to ‘freeze’ a set of packages ‣ and re-install to the same level http://pypi.python.org/pypi/pip
  39. 39. django-debug-toolbar http://localhost:8000/tutorial/
  40. 40. django-command-extensions ‣ Extra manage.py commands ‣ shell_plus : a better python shell ‣ runserver_plus : a better debugging server (werkzeug) ‣ show_urls : dump the url map of your site
  41. 41. Werkzeug http://localhost:8000/tutorial/latest/5
  42. 42. and of course, unittest ‣ Django supports : ‣ doctest : useful for simple model validation ‣ unittest : you did write some, didn’t you ? ‣ test client : acts a dummy web browser ‣ Test your views ‣ fixture loading : have a set of complex test data ‣ generated from your production database
  43. 43. reusable apps if it’s useful, it’s probably been done before...
  44. 44. south ‣ Schema migration ‣ Change models over time ‣ Write upgrade routines ‣ just python functions with access to .objects and .old_objects
  45. 45. A platform for rapidly developing (social) websites
  46. 46. code.google.com ‣ just search for django-<WHATEVER> :) ‣ It’s probably there... ‣ If it’s not there, write it and put it there
  47. 47. Future beyond django 1.1
  48. 48. Multi-DB ‣ Allows your models to be in multiple DBs ‣ Different applications in different DBs ‣ ‘sharding’ of objects across DBs ‣ using slaves for read-only operations
  49. 49. Other ‣ Admin UI enhancements ‣ autocompletion ‣ better inline handling ‣ Non-relational database support ‣ CouchDB, MongoDB, tokyo Tyrant, Google Bigtable, SimpleDB
  50. 50. want more ?
  51. 51. ‣ http://docs.djangoproject.com/ ‣ Django community RSS feed ‣ http://www.djangoproject.com/community/ ‣ Mailing lists ‣ django-dev to understand how the developers think ‣ django-users to ask for help ‣ DjangoCon presentations ‣ http://www.djangocon.org/
  52. 52. Books
  53. 53. django-users@cern.ch
  54. 54. Thank you
  55. 55. Credits ‣ XKCD for cartoons ‣ Amazon.com for book pictures ‣ Initial inspiration for slides and examples from Joaquim Rocha, Abe Estrada ‣ http://www.slideshare.net/j_rocha/django-intro ‣ http://www.slideshare.net/AbeEstrada/django-web-framework-presentation-822177 ‣ http://www.djangoproject.com/
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×