From 0 to Website in 60 Minutes - with Django

Im going to Linuxfest Northwest 2010 April 24-25th

Sean Boisen, <[my first name]>

LinuxFest Northwest 2010, April 24

Presentation URL:

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. Creative Commons License



Why Django?


Shortcuts: What I'm Leaving Out

The Application: F3

The Application: F3 (2)

The Source Data: Farms

graphic of a sample farm

The Source Data: Berries

graphic of a seasonal guide fragment

Data Elements of F3

Building the App: startapp

Building the App: Our First View

Building the App: Testing Our First View

Review of our First App

The Database Layer

Defining Models: FoodType, Month

import django.db.models as m

class FoodType(m.Model):
    name = m.CharField(max_length=30, unique=True)

    def __unicode__(self):

class Month(m.Model):
    name = m.CharField(max_length=10, unique=True,
                       help_text="the month")
    order = m.IntegerField(unique=True)

    def __unicode__(self):

Defining Models: Food

class Food(m.Model):
    name = m.CharField(max_length=50, unique=True,
                       help_text="the name of the food")
    type = m.ForeignKey('FoodType')
    months = m.ManyToManyField('Month')

    def __unicode__(self):

Defining Models: Farm (Abbreviated)

class Farm(m.Model):
    name = m.CharField(max_length=100)
    contact_person = m.CharField(max_length=100)
    address = m.TextField()
    phone_number = m.CharField(max_length=30)
    website = m.URLField()
    email_address = m.EmailField()
    description = m.TextField()
    hours_of_operation = m.CharField(max_length=100)
    seasonal_operation = m.CharField(max_length=100)
    service_offerings = m.ManyToManyField('ServiceOffering')
    payment_methods = m.ManyToManyField('PaymentMethod')
    sustainability_indicators = m.ManyToManyField('SustainabilityIndicator')
    categories = m.ManyToManyField('FarmCategory')
    confirmed_foods = m.ManyToManyField('Food')

Loading Data

import django.core.serializers as ser

for obj in ser.deserialize("json", open('f3_final.json', 'r')):

A Snippet of Data in JSON

  { "pk": 49, 
    "model": "", 
    "fields": {
      "phone_number": "360.380.2699", 
      "website": "", 
      "name": "Boxx Berry Farm", 
      "confirmed_foods": [9, 8, 2, 13, 46, 60], 
      "service_offerings": [1, 5, 6], 
      "hours_of_operation": "", 
      "seasonal_operation": "", 
      "payment_methods": [], 
      "address": "6211 Northwest Road\nFerndale", 
      "sustainability_indicators": [1], 
      "contact_person": "Mike & Roger Boxx", 
      "email_address": "", 
      "categories": [], 
      "description": ""

Database API: Simple Query

>>> from f3_final.models import *
>>> bbf = Farm.objects.get(id=49)
>>> bbf
<Farm: Boxx Berry Farm>
>>> FoodType.objects.all()
[<FoodType: Berries>, <FoodType: Dairy Products>, 
<FoodType: Eggs> ... ]

Database API: Other Queries

>>> Month.objects.filter(name__contains='r')
[<Month: January>, <Month: February>, 
<Month: March>, <Month: April>, <Month: September>, 
<Month: October>, <Month: November>, <Month: December>]
>>> Food.objects.filter(type__name='Berries')
[<Food: Blackberries>, <Food: Blueberries>, 
<Food: Boysenberries> ... ]

More Data Layer Functionality

Template Basics

My First Template

<head><title>My First Template</title></head>
	{% if object_list %}
		{% for object in object_list %}
		  <td>{{ }}</td>
		  <td>{{ }}</td>
		{% endfor %}
	{% endif %}

Rendering Views with Templates

Putting It All Together: f3_final

Putting It All Together: f3_final (2)

Admin Interface

Review: The Django Development Cycle

What Else Can Django Do?

Best Practices for Speedy Django Development

And Now, a Word from our Sponsors ...