Project 3 - Django database
Checkpoint due 10pm Wednesday May 14
Final project due 10pm Tuesday May 20

Here is example code for the checkpoint.

We'll use Django in this project to set up a database for our pedometer data. The database will include two tables, one of information about the users, and the other of the pedometer data itself. By the 13th, you should have set up the database and produced a Django user registration form. By the 20th, you should have added in the final pedometer data and a form to query the database.

We'll collect pedometer data until Thursday 5/15. Start today (5/5). Read the instructions on the pedometer. Make sure it is set to 24hr mode (use the set button) and that it has the correct time (so that it flips at midnight). We'll collect the data using Alisha's version of the pedometer data collection app from last time.

Make up a userid consisting of five letters or digits. Feel free to make it recognizable as you, or not. Please use this ID consistently when entering your data.

Django tutorial

Now, we'll get started on the database using Django. Django is already installed. You should have a "mysite" directory in your /var/www/yourname directory. That will be your main Django directory. Check that Django is working and producing Web pages, as described here. You should already have all the code described in the tutorial in a directory /mysite/polls.

Work through the following sections "Writing your first Django app" tutorial. Start here in the section on making models. You can skip Part 2. In Part 3, start at the beginning and go through until you fininsh the section: "A shortcut: render()", a little more than half-way down. Finally, in Part 4, start at the beginning and go till "Use generic views: Less code is better".

Wherever it tells you to run the development server in the tutorial, we will instead be using our Web server. A page whose URL is "http://127.0.0.1:8000/admin/", in the tutorial, for instance, will have URL "http:pc110.cs.ucdavis.edu:10000/django/admin/" on our machine (where 10000 is your 5-digit number).

First part of assignment - User database and Web site

Start your own Web app in directory /mysite/steps, analogous to /mystite/polls. Make a database table for the user data by making a class called User. The table should have two columns, one for userid and one for how the user gets to school (which should be a code indicating one of bike, walk, bus or car).

Finally, make the Web pages for users to register. This will fill in rows/instances in the user table. The first page, /django/steps/uidForm, should be a form that accepts a userid, and have a pull-down menu or radio buttons for the way the user gets to school (bus, car, bike or walk). Store this data in the user table in the database.

For the checkpoint, we will look at your /steps/ url and make sure the form is there.

Making a Database for Pedometer Data

Our next step is to take the data we collected in steps.py and put it into a Django database. Here is steps.csv.

First, add the following model for pedometer data to models.py.

class Pedometer(models.Model):
    user = models.ForeignKey(User)
    steps = models.PositiveIntegerField()
    month = models.PositiveIntegerField()
    day = models.PositiveIntegerField()

Remember that when you change model.py you need to re-run the "python manage.py syncdb" command to actually create the database table.

Now lets get the data from 'steps.csv' into this database table. We do this with a Python program that goes into directory /var/www/yourname/mysite. Let's call it loadPedometer.py. Here's the beginning of this program, that imports the Django models:

from django.core.management import setup_environ
from mysite import settings

setup_environ(settings)

from steps.models import User, Pedometer

#u = User(uid="garp", transport="k")
#u.save()

You can comment out the last two lines to see how it can add something to a table. Run it by going to /var/www/yourname/mysite and typing "python loadPedometer.py". Change it so that it fills up the Pedometer table by creating an instance of the Pedometer class for each line of 'steps.csv'. Don't forget that you have to save each new object instance that you create to make it go into the database.

Displaying User and Pedometer Data on a Web page

Let's program one more Web interaction, to let people look at our pedometer data and our user data together. Make a form Web page like this:

that lets someone ask about all the records for a particular user. When they hit the submit button, they should get back information like this:

These are all the records for this user in the pedometer database; notice it also shows the user's preferred mode of transportation.

References