From 0 to Website in 60 Minutes - with Django

Im going to Linuxfest Northwest 2010 April 24-25th

Sean Boisen, <[my first name]@seanboisen.com>

LinuxFest Northwest 2010, April 24

Presentation URL: http://semanticbible.org/other/talks/2010/linuxfestnw/DjangoIntro.html

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

Outline

Goals

Why Django?

Prerequisites

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):
        return self.name

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

    def __unicode__(self):
        return self.name

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):
        return self.name

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')):
    obj.save()

A Snippet of Data in JSON

  { "pk": 49, 
    "model": "f3_final.farm", 
    "fields": {
      "phone_number": "360.380.2699", 
      "website": "http://www.boxxberryfarm.com", 
      "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>
>>> bbf.website
u'http://www.boxxberryfarm.com'
>>> 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

<html>
<head><title>My First Template</title></head>
<body>
    <h1></h1>
	{% if object_list %}
	<table>
	  <thead><tr><th>ID</th><th>Name</th></tr></thead>
	  <tbody>
		{% for object in object_list %}
		<tr>
		  <td>{{ object.id }}</td>
		  <td>{{ object.name }}</td>
		</tr>
		{% endfor %}
	  </tbody>
	</table>
	{% endif %}
</body>
</html>

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 ...

Resources