Jumpstart


                  The Web Framework
            for Perfectionists with Deadlines


                                         CPOSC
Rob Yates                                October, 2009
Topics
Topics
• What is Django
Topics
• What is Django
• History of Django
Topics
• What is Django
• History of Django
• Whirlwind Tour
Topics
• What is Django
• History of Django
• Whirlwind Tour
• How to Get Started - Admin Demo
Topics
• What is Django
• History of Django
• Whirlwind Tour
• How to Get Started - Admin Demo
• Killer Features
Topics
• What is Django
• History of Django
• Whirlwind Tour
• How to Get Started - Admin Demo
• Killer Features
• Application Re-use and Pinax
Topics
• What is Django
• History of Django
• Whirlwind Tour
• How to Get Started - Admin Demo
• Killer Features
• Application Re-use and Pinax
• Framework Death-match
What is Django?
What is Django?
•   Not a Gypsy Jazz Guitarist
What is Django?
•   Not a Gypsy Jazz Guitarist

•   Web Application
    Development Framework
    written in and for Python
What is Django?
•   Not a Gypsy Jazz Guitarist

•   Web Application
    Development Framework
    written in and for Python

•   Not a CMS System
What is Django?
•   Not a Gypsy Jazz Guitarist

•   Web Application
    Development Framework
    written in and for Python

•   Not a CMS System

•   Sometimes called Python’s
    Ruby on Rails (RoR)
It all started
                          HERE




Photo by Jeff Croft
Simon Willison




Adrian Holovaty
PyCon 2005
Major topic: Why is web development
         in Python so hard?
Jump to today:
Guiding Principles
Jump to today:
    Guiding Principles
• Loose Coupling and tight cohesion
Jump to today:
    Guiding Principles
• Loose Coupling and tight cohesion
• Less code
Jump to today:
    Guiding Principles
• Loose Coupling and tight cohesion
• Less code
• Quick development
Jump to today:
    Guiding Principles
• Loose Coupling and tight cohesion
• Less code
• Quick development
• DRY
Jump to today:
    Guiding Principles
• Loose Coupling and tight cohesion
• Less code
• Quick development
• DRY
• Explicit is better than implicit
Jump to today:
    Guiding Principles
• Loose Coupling and tight cohesion
• Less code
• Quick development
• DRY
• Explicit is better than implicit
• Consistency
For Free!!!
           • Very Useful Admin CRUD
           • Templating - lots of useful rendering bits
           • Form Handling
           • i18n - full Unicode support
           • Sessions / User Auth / Role-based Perms
           • Object-Relational Mapping
Beer photo by Lori Spindler
Model View Controller
Model View Controller

            Model
            View
            Template
Models
Templates




            App
 Views

 Models
Templates
            App

 Views
                  Site




 Models
Templates
            App




 Views

    Settings
     URLs
                         Django App Structure




   Templates
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Deep Dive: Models
from tagging.fields import TagField

class Post(models.Model):
  author = models.ForeignKey(Author)
  title = models.CharField(max_length=200)
  slug = models.SlugField(max_length=200, unique_for_month='pub_date')
  body = models.TextField()
  TYPE_CHOICES = (
    ('rant', 'Rant'),
    ('tirade', 'Tirade'),
  )
  type = models.CharField(choices=TYPE_CHOICES, max_length=50)
  pub_date = models.DateTimeField(auto_now_add=True)
  is_active = models.BooleanField(default=True)
  tags = TagField(blank=True, null=True)

  class Meta:
    unique_together = ('title', 'type')
    ordering = ['-pub_date']
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Deep Dive: DB API
post = Post(title=‘Foo’, body=‘Bar’)
post.type = ‘rant’
post.save()
Deep Dive: DB API
post = Post(title=‘Foo’, body=‘Bar’)
post.type = ‘rant’
post.save()

posts = Post.objects.all()

posts = Post.objects.all()[:5]

posts = Post.objects.filter(post_type=‘rant’)

posts = Post.objects.filter(author__name=‘Rob’)

posts = Post.objects.filter(author__name=‘Rob’).
    filter(title__contains=‘django’).order_by(‘type’)

post = Post.objects.get(id=24)
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Deep Dive: Forms
# Basic Form
class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)
Deep Dive: Forms
# Basic Form
class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)



# Form based on a model
class PostForm(forms.ModelForm):
    class Meta:
        model = Post
Deep Dive: Views
def posts_by_type(request, type):
    posts = Post.objects.filter(type=type)

    return render_to_response(
            ‘postsbytype.html’, {‘posts’: posts})
Deep Dive: Views
def posts_by_type(request, type):
    posts = Post.objects.filter(type=type)

    return render_to_response(
            ‘postsbytype.html’, {‘posts’: posts})


def new_post(request):
    if request.method == ‘POST’:
        form = PostForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = PostForm()
    return render_to_response(
            ‘mytemplate.html’, {‘form’: form})
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Deep Dive: URLs
urlpatters = patterns(‘’,
    (’^my/favorite/color/$’, ‘blog.views.my_fav_color’),
    (‘^post/new/$’, ‘blog.views.new_post’),



    (‘^posts/(d{4})/$’, ‘blog.views.year_archive’),
    (‘^posts/(d{4})/(d{2})/$’, ‘blog.views.month_archive’),



    (‘^posts/(?P<slug>[a-z-]*)/$’, ‘blog.views.show_post’),



    (‘^events/$’, include(events.urls),
)
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Deep Dive: Templates
<html><body>
   {% if posts %}
      {% for post in posts %}
         {{ post.author }}: {{ post.title }}
      {% endfor %}
   {% endif %}
Deep Dive: Templates
<html><body>
   {% if posts %}
      {% for post in posts %}
         {{ post.author }}: {{ post.title }}
      {% endfor %}
   {% endif %}


<form method=”post”>
   <table>
      {{ form }}
   </table>
</form>
Deep Dive: Templates
<html><body>
   {% if posts %}
      {% for post in posts %}
         {{ post.author }}: {{ post.title }}
      {% endfor %}
   {% endif %}


<form method=”post”>
   <ul>
      {{ form.as_ul }}
   </ul>
</form>
Architecture
                       RDBMS

   Storage             ORM        Fixtures

                       Model

Signals        Forms              Template

URL Resolver           View    Template Loader


 Middleware                      Middleware

   Request                        Response
Getting Started
• Pure Python - no compiled code
• No dependencies (with the exception of
  the database adapter of your choice
• Three-step install
 • D/L and extract tar
 • Run setup.py
 • Add django-admin.py to path (optional)
Initialize Your Project
$ django-admin.py startproject mysite
$ cd mysite
$ python manage.py startapp myapp
$ edit settings.py and set DB Info
$ python manage.py runserver


        http://localhost:8000
Killer Features
•   Legacy DB            •   Generic Views

•   Test Framework       •   Template Inheritance

•   Multi-DB             •   Tempalte Filters

•   Geodjango            •   Humanize

•   Aggregation          •   Runs on Jython /
                             IronPython
•   JSON Serializer

•   Stable API Promise   •   Google AppEngine!

•   Fantastic Docs
Roll Your Own
• Model Managers: Custom database API for views
• Custom Template Tags: Easier to implement
  functionality without mixing logic/presentation
Roll Your Own
• Model Managers: Custom database API for views
• Custom Template Tags: Easier to implement
  functionality without mixing logic/presentation


  • ORM Layer       • Model Fields
  • Template Engine • Form Fields
  • Template Loader • URL Resolver
Re-usable Apps
         and Pinax
• Following best practices when creating
  your app allows others to plug in and use it
• Search Google Code for *django* projects
  returns 1,987 results - many are apps
• James Tauber created Django Hotclub to
  establish standards for app development
  and integration
• Hotclub later become Pinax
Framework Death-match




Photo by Bruce Turner
Death-match: RoR

•   URL Handling

    •   RoR: RESTful with HTTP verbs

    •   Django: RegEx-based URL patterns

•   JavaScript / AJAX

    •   RoR: XHR helpers with RJS, prototype, scriptaculous

    •   Django: JS framework agnostic / no helpers besides
        serialization
Death-match: RoR

•   Unicode

    •   Ruby traditionally has had issues - string is byte sequence

    •   Python is fully Unicode since v1.6 (circa 2000)

•   i18n

    •   RoR: Translations are copied - no fallback?

    •   Django: Translations use GNU gettext-style localization -
        falls back to default language
Death-match: RoR

•   Plugins

    •   RoR: Rich plugin API and structure

    •   Django: Re-usable applications

•   Automation / Build

    •   RoR: rake is a rich scripting language for automated tasks

    •   Django: manage.py can do some basic tricks - CLI
Death-match: RoR
•   Jobs: Rails jobs out-number Django jobs 6:1

•   Hype: Rails has done a fantastic job with marketing
Death-match: RoR
•   Jobs: Rails jobs out-number Django jobs 6:1

•   Hype: Rails has done a fantastic job with marketing
Recap
Recap
• Django is easy
Recap
• Django is easy
• Django is fun
Recap
• Django is easy
• Django is fun
• Django scales
Recap
• Django is easy
• Django is fun
• Django scales
• Django is maintainable
Recap
• Django is easy
• Django is fun
• Django scales
• Django is maintainable
• Django saves small kittens
Recap
• Django is easy
• Django is fun
• Django scales
• Django is maintainable
• Django saves small kittens
• It rocks - USE IT!
Resources
• http://djangoproject.com
 • Check out the tutorial!
• http://docs.djangoproject.com
• http://djangodose.com
• http://www.b-list.org/
• http://pinaxproject.com
• http://code.google.com/hosting/search?
  q=label%3aDjango
Thanks!
 rob@robyates.org

Twitter: @Rob_Yates
 http://robyates.org

Jumpstart Django

  • 1.
    Jumpstart The Web Framework for Perfectionists with Deadlines CPOSC Rob Yates October, 2009
  • 2.
  • 3.
  • 4.
    Topics • What isDjango • History of Django
  • 5.
    Topics • What isDjango • History of Django • Whirlwind Tour
  • 6.
    Topics • What isDjango • History of Django • Whirlwind Tour • How to Get Started - Admin Demo
  • 7.
    Topics • What isDjango • History of Django • Whirlwind Tour • How to Get Started - Admin Demo • Killer Features
  • 8.
    Topics • What isDjango • History of Django • Whirlwind Tour • How to Get Started - Admin Demo • Killer Features • Application Re-use and Pinax
  • 9.
    Topics • What isDjango • History of Django • Whirlwind Tour • How to Get Started - Admin Demo • Killer Features • Application Re-use and Pinax • Framework Death-match
  • 10.
  • 11.
    What is Django? • Not a Gypsy Jazz Guitarist
  • 12.
    What is Django? • Not a Gypsy Jazz Guitarist • Web Application Development Framework written in and for Python
  • 13.
    What is Django? • Not a Gypsy Jazz Guitarist • Web Application Development Framework written in and for Python • Not a CMS System
  • 14.
    What is Django? • Not a Gypsy Jazz Guitarist • Web Application Development Framework written in and for Python • Not a CMS System • Sometimes called Python’s Ruby on Rails (RoR)
  • 15.
    It all started HERE Photo by Jeff Croft
  • 16.
  • 18.
    PyCon 2005 Major topic:Why is web development in Python so hard?
  • 19.
  • 20.
    Jump to today: Guiding Principles • Loose Coupling and tight cohesion
  • 21.
    Jump to today: Guiding Principles • Loose Coupling and tight cohesion • Less code
  • 22.
    Jump to today: Guiding Principles • Loose Coupling and tight cohesion • Less code • Quick development
  • 23.
    Jump to today: Guiding Principles • Loose Coupling and tight cohesion • Less code • Quick development • DRY
  • 24.
    Jump to today: Guiding Principles • Loose Coupling and tight cohesion • Less code • Quick development • DRY • Explicit is better than implicit
  • 25.
    Jump to today: Guiding Principles • Loose Coupling and tight cohesion • Less code • Quick development • DRY • Explicit is better than implicit • Consistency
  • 26.
    For Free!!! • Very Useful Admin CRUD • Templating - lots of useful rendering bits • Form Handling • i18n - full Unicode support • Sessions / User Auth / Role-based Perms • Object-Relational Mapping Beer photo by Lori Spindler
  • 27.
  • 28.
    Model View Controller Model View Template
  • 29.
    Models Templates App Views Models Templates App Views Site Models Templates App Views Settings URLs Django App Structure Templates
  • 30.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 31.
    Deep Dive: Models fromtagging.fields import TagField class Post(models.Model): author = models.ForeignKey(Author) title = models.CharField(max_length=200) slug = models.SlugField(max_length=200, unique_for_month='pub_date') body = models.TextField() TYPE_CHOICES = ( ('rant', 'Rant'), ('tirade', 'Tirade'), ) type = models.CharField(choices=TYPE_CHOICES, max_length=50) pub_date = models.DateTimeField(auto_now_add=True) is_active = models.BooleanField(default=True) tags = TagField(blank=True, null=True) class Meta: unique_together = ('title', 'type') ordering = ['-pub_date']
  • 32.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 33.
    Deep Dive: DBAPI post = Post(title=‘Foo’, body=‘Bar’) post.type = ‘rant’ post.save()
  • 34.
    Deep Dive: DBAPI post = Post(title=‘Foo’, body=‘Bar’) post.type = ‘rant’ post.save() posts = Post.objects.all() posts = Post.objects.all()[:5] posts = Post.objects.filter(post_type=‘rant’) posts = Post.objects.filter(author__name=‘Rob’) posts = Post.objects.filter(author__name=‘Rob’). filter(title__contains=‘django’).order_by(‘type’) post = Post.objects.get(id=24)
  • 35.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 36.
    Deep Dive: Forms #Basic Form class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() cc_myself = forms.BooleanField(required=False)
  • 37.
    Deep Dive: Forms #Basic Form class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField() sender = forms.EmailField() cc_myself = forms.BooleanField(required=False) # Form based on a model class PostForm(forms.ModelForm): class Meta: model = Post
  • 38.
    Deep Dive: Views defposts_by_type(request, type): posts = Post.objects.filter(type=type) return render_to_response( ‘postsbytype.html’, {‘posts’: posts})
  • 39.
    Deep Dive: Views defposts_by_type(request, type): posts = Post.objects.filter(type=type) return render_to_response( ‘postsbytype.html’, {‘posts’: posts}) def new_post(request): if request.method == ‘POST’: form = PostForm(request.POST) if form.is_valid(): form.save() else: form = PostForm() return render_to_response( ‘mytemplate.html’, {‘form’: form})
  • 40.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 41.
    Deep Dive: URLs urlpatters= patterns(‘’, (’^my/favorite/color/$’, ‘blog.views.my_fav_color’), (‘^post/new/$’, ‘blog.views.new_post’), (‘^posts/(d{4})/$’, ‘blog.views.year_archive’), (‘^posts/(d{4})/(d{2})/$’, ‘blog.views.month_archive’), (‘^posts/(?P<slug>[a-z-]*)/$’, ‘blog.views.show_post’), (‘^events/$’, include(events.urls), )
  • 42.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 43.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 44.
    Deep Dive: Templates <html><body> {% if posts %} {% for post in posts %} {{ post.author }}: {{ post.title }} {% endfor %} {% endif %}
  • 45.
    Deep Dive: Templates <html><body> {% if posts %} {% for post in posts %} {{ post.author }}: {{ post.title }} {% endfor %} {% endif %} <form method=”post”> <table> {{ form }} </table> </form>
  • 46.
    Deep Dive: Templates <html><body> {% if posts %} {% for post in posts %} {{ post.author }}: {{ post.title }} {% endfor %} {% endif %} <form method=”post”> <ul> {{ form.as_ul }} </ul> </form>
  • 47.
    Architecture RDBMS Storage ORM Fixtures Model Signals Forms Template URL Resolver View Template Loader Middleware Middleware Request Response
  • 48.
    Getting Started • PurePython - no compiled code • No dependencies (with the exception of the database adapter of your choice • Three-step install • D/L and extract tar • Run setup.py • Add django-admin.py to path (optional)
  • 49.
    Initialize Your Project $django-admin.py startproject mysite $ cd mysite $ python manage.py startapp myapp $ edit settings.py and set DB Info $ python manage.py runserver http://localhost:8000
  • 50.
    Killer Features • Legacy DB • Generic Views • Test Framework • Template Inheritance • Multi-DB • Tempalte Filters • Geodjango • Humanize • Aggregation • Runs on Jython / IronPython • JSON Serializer • Stable API Promise • Google AppEngine! • Fantastic Docs
  • 51.
    Roll Your Own •Model Managers: Custom database API for views • Custom Template Tags: Easier to implement functionality without mixing logic/presentation
  • 52.
    Roll Your Own •Model Managers: Custom database API for views • Custom Template Tags: Easier to implement functionality without mixing logic/presentation • ORM Layer • Model Fields • Template Engine • Form Fields • Template Loader • URL Resolver
  • 53.
    Re-usable Apps and Pinax • Following best practices when creating your app allows others to plug in and use it • Search Google Code for *django* projects returns 1,987 results - many are apps • James Tauber created Django Hotclub to establish standards for app development and integration • Hotclub later become Pinax
  • 55.
  • 56.
    Death-match: RoR • URL Handling • RoR: RESTful with HTTP verbs • Django: RegEx-based URL patterns • JavaScript / AJAX • RoR: XHR helpers with RJS, prototype, scriptaculous • Django: JS framework agnostic / no helpers besides serialization
  • 57.
    Death-match: RoR • Unicode • Ruby traditionally has had issues - string is byte sequence • Python is fully Unicode since v1.6 (circa 2000) • i18n • RoR: Translations are copied - no fallback? • Django: Translations use GNU gettext-style localization - falls back to default language
  • 58.
    Death-match: RoR • Plugins • RoR: Rich plugin API and structure • Django: Re-usable applications • Automation / Build • RoR: rake is a rich scripting language for automated tasks • Django: manage.py can do some basic tricks - CLI
  • 59.
    Death-match: RoR • Jobs: Rails jobs out-number Django jobs 6:1 • Hype: Rails has done a fantastic job with marketing
  • 60.
    Death-match: RoR • Jobs: Rails jobs out-number Django jobs 6:1 • Hype: Rails has done a fantastic job with marketing
  • 61.
  • 62.
  • 63.
    Recap • Django iseasy • Django is fun
  • 64.
    Recap • Django iseasy • Django is fun • Django scales
  • 65.
    Recap • Django iseasy • Django is fun • Django scales • Django is maintainable
  • 66.
    Recap • Django iseasy • Django is fun • Django scales • Django is maintainable • Django saves small kittens
  • 67.
    Recap • Django iseasy • Django is fun • Django scales • Django is maintainable • Django saves small kittens • It rocks - USE IT!
  • 68.
    Resources • http://djangoproject.com •Check out the tutorial! • http://docs.djangoproject.com • http://djangodose.com • http://www.b-list.org/ • http://pinaxproject.com • http://code.google.com/hosting/search? q=label%3aDjango
  • 69.

Editor's Notes

  • #2 Survey: How many do web application development; in django; in rails
  • #14 Lawrence, Kansas Since 1850&amp;#x2019;s Family run newspaper - currently run by someone who is 70 years old that does not use a computer - assistant prints out e-mails
  • #15 Adrian: Chicago Crime, Coined the term &amp;#x201C;Database Journalism&amp;#x201D; Simon: Early work is part of many JS frameworks, Consultant on OpenID Joined by Jacob Kaplan-Moss
  • #16 First online newspaper of it&amp;#x2019;s kind. Had a print edition of the website
  • #17 &amp;#x201C;The CMS&amp;#x201D;
  • #19 In a landscape of confusion - invent your own
  • #20 Re-framed the perception of web development;
  • #21 Various layers shouldn&amp;#x2019;t know about eachother unless absolutely necessary Code should lack boilerplate - Java Make tedious aspects fast - it&amp;#x2019;s just Python shouldn&amp;#x2019;t do much magic
  • #22 Various layers shouldn&amp;#x2019;t know about eachother unless absolutely necessary Code should lack boilerplate - Java Make tedious aspects fast - it&amp;#x2019;s just Python shouldn&amp;#x2019;t do much magic
  • #23 Various layers shouldn&amp;#x2019;t know about eachother unless absolutely necessary Code should lack boilerplate - Java Make tedious aspects fast - it&amp;#x2019;s just Python shouldn&amp;#x2019;t do much magic
  • #24 Various layers shouldn&amp;#x2019;t know about eachother unless absolutely necessary Code should lack boilerplate - Java Make tedious aspects fast - it&amp;#x2019;s just Python shouldn&amp;#x2019;t do much magic
  • #25 Various layers shouldn&amp;#x2019;t know about eachother unless absolutely necessary Code should lack boilerplate - Java Make tedious aspects fast - it&amp;#x2019;s just Python shouldn&amp;#x2019;t do much magic
  • #26 Various layers shouldn&amp;#x2019;t know about eachother unless absolutely necessary Code should lack boilerplate - Java Make tedious aspects fast - it&amp;#x2019;s just Python shouldn&amp;#x2019;t do much magic
  • #60 manage.py syncdb
  • #61 Those too hip for non-relational databases will have to wait. Multi-DB support is coming.
  • #65 Notice: more declarative programming
  • #66 Notice: more declarative programming
  • #70 You can generate these things dynamically
  • #71 Middleware
  • #72 Middleware
  • #73 Middleware
  • #74 Complete separation of logic and presentation - pain for things like Math or comparisons. Templates don&amp;#x2019;t need to be HTML - JSON, e-mail, text
  • #75 Complete separation of logic and presentation - pain for things like Math or comparisons. Templates don&amp;#x2019;t need to be HTML - JSON, e-mail, text
  • #76 Complete separation of logic and presentation - pain for things like Math or comparisons. Templates don&amp;#x2019;t need to be HTML - JSON, e-mail, text
  • #77 Complete separation of logic and presentation - pain for things like Math or comparisons. Templates don&amp;#x2019;t need to be HTML - JSON, e-mail, text
  • #85 PHP comparison doesn&amp;#x2019;t count. Really a Django Rails comparison
  • #91 Who is the clear winner?
  • #92 Curl.com
  • #93 Curl.com
  • #94 Curl.com
  • #95 Curl.com
  • #96 Curl.com
  • #97 Curl.com