Introduction to Django

For Perfectionists with Deadlines

A talk by Travis Swicegood / @tswicegood / #djintro

House Keeping

Questions: Ask Them

Twitter!

@tswicegood | #djintro

Slides are Online

Link at the end, so hold your horses

About Me

Texas Tribune

I All Hate Software

What Is It?

Rapid Application Development Framework

Provides a Vocabulary

Opiniated, but…

Scalable

Questions?

Glossary

Settings

Router

Models via ORM

Templating

… and so much more

Questions?

How it Works

Executing Code

WSGI

Production Deployment

Projects & Apps

Questions?

Settings

All the Wires

Kinda Awful

Kinda Awesome

Required Bits

  • Database Config
  • Installed Apps

Database Config


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': './project.db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}
            

Installed Apps


INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',

    # ... and so on
    'your_app',
    'third_party_app',
]
            

Questions?

Models

Simple Person


from django.db import models


class Person(models.Model):
    name = models.CharField(max_length=250)
    language = models.CharField(max_length=2)
            

Creating a Model


$ python manage.py shell
Python 2.7.3 (default, Apr 20 2012, 18:35:54)
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from people.models import Person
>>> t = Person.objects.create(name='Travis Swicegood', language='en')
>>> t.name
'Travis Swicegood'
>>> t
<Person: Person object>
>>>
            

Better Representation


class Person(models.Model):
    name = models.CharField(max_length=250)
    language = models.CharField(max_length=2)

    def __unicode__(self):
        return self.name
            

Getting a Person


>>> from people.models import Person
>>> t = Person.objects.get(name='Travis Swicegood')
>>> t
<Person: Travis Swicegood>
>>>
            

Finding a Person


>>> Person.objects.filter(name='Travis Swicegood')
[<Person: Travis Swicegood>]
            

Without Relationships


class Person(models.Model):
    name = models.CharField(max_length=250)
    language = models.CharField(max_length=2)

    def __unicode__(self):
        return self.name
            

Handling Relationships


class Language(models.Model):
    name = models.CharField(max_length=250)
    code = models.CharField(max_length=2)

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.code)


class Person(models.Model):
    name = models.CharField(max_length=250)
    language = models.ForeignKey(Language, related_name='people')

    def __unicode__(self):
        return self.name
            

Relationships in the Shell


>>> from people.models import *
>>> en = Language.objects.create(name='English', code='en')
>>> en
<Language: English (en)>
>>> t = Person.objects.create(name='Travis Swicegood', language=en)
>>> t
<Person: Travis Swicegood>
>>> t.language
<Language: English (en)>
>>>
            

Handling Reverse


  >>> en.people.all()
  [<Person: Travis Swicegood>]
  >>> en.people.get(name='Travis Swicegood') == t
  True
            

But Wait!

There's More!

Questions?

Admin

INSTALLED_APPS


# Uncomment the next line to enable the admin:
'django.contrib.admin',
            

urls.py


# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

# ... further down the file

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
            

people.admin


from django.contrib import admin

from . import models

admin.site.register(models.Language)
admin.site.register(models.Person)
            

Demo Time!

Remember!

Admin != Your App

Questions?

Routing, Views, & Templates

Routing

Project Routes


from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),

    url('person/', include('people.urls')),
)
            

App Routes


from django.conf.urls import patterns, url

from . import views

urlpatterns = patterns('',
    url(r'^$', views.PeopleListView.as_view(), name="people_list"),
    url(r'^(?P[0-9]+)/$', views.PersonView.as_view(), name="person"),
)
            

Views

Class Based Views

CBVs

Example Views


from django.views.generic import DetailView, ListView

from . import models


class PeopleListView(ListView):
    model = models.Person


class PersonView(DetailView):
    model = models.Person
            

Seriously…

Templates

Templates and CBVs


{{ app_name }}/{{ model_name }}_detail.html
{{ app_name }}/{{ model_name }}_list.html
            

people/person_detail.html
people/person_list.html
            

person_detail.html


<dl>
  <dt>Name</dt>
  <dd>{{ object.name }}</dd>

  <dt>Language</dt>
  <dd>{{ object.language }}</dd>
</dl>
            

person_list.html


<ul>
{% for person in object_list %}
  <li><a href="{% url "person" pk=person.pk %}">{{ person }}</a></li>
{% endfor %}
</ul>
            

So much more…

Questions?

Ecosystem

Third Party

  • API: Tastypie
  • Search: Haystack
  • Queue: Celery
  • Features: Reusable Apps

Questions?

@tswicegood | #djintro

tswicegood.github.io/intro-to-django/