Django Tutorial: Part I

Today we’re going to create a Song model, set up a few songs, and then develop our list and search functionality for our Songster application. Future tutorials will include search, add and edit functionality and more.

First, let’s do some necessary preliminary setup. cd into your songster directory, and open the file in your text editor. is were your model objects will live. In, enter the following code:

from django import models

class Song(models.Model):
    title = models.CharField(max_length=100)
    artist = models.CharField(max_length=200)

This is a song object, which inherits from Django’s Model object. We have defined two fields for Song: it will have a title and an artist, both comprised of strings. Go ahead and save your Song object.

To use this model, we need to activate it in our project. Open the file in the songs directory in a text editor. Find the INSTALLED_APPS section and enter the following line before the closing bracket:


Let’s sync up the database now. Enter the following at the command prompt:

python syncdb

It should report “Creating table songster_song”. Whenever you add a data model, you need to run this command in order to create its table in the database.

Now, let’s use the django shell to save off some song objects in the database, so we can move on with our list functionality. From a command line, enter:

python shell

This will start the django interactive interpreter, which is kind of like the Python interactive interpreter, but with access to a lot of django goodies.

Enter the following at the shell prompt:

from songster.models import *

This allows us to refer to song model objects in the shell.
Then, enter the following:

>>> song1 = Song(title='Promise of Summer', artist='Jackopierce')
>>> song2 = Song(title='Save it for a Rainy Day', artist='Jayhawks')

Calling save() on each song object stores the song in the database.

Now that we have a couple of songs saved off, we can move out of our preliminary setup work and get on with the real business of coding up our list functionality!

1. Add an entry in for your view method
Open up in a text editor, and find the urlpatterns section, then enter the following:

from songster import views
(r'^list/$', views.list), 

This says that any url ‘list’ will be mapped to the list method in

2. Create a list method in that gets the objects from the database.
Open up in a text editor. At the top of the file, enter the following lines:

from django.shortcuts import render_to_response
from django.template import RequestContext
from songster.models import Song

Then, create a list method that looks like the following:

def list(request):
    songs = Song.objects.all()

3. Put the song objects in variables and return them.
Add the following lines to your list method:

    variables = RequestContext(request, {'songs' : songs})
    return render_to_response('list_results.html', variables)

4. Create a list results html page and display the list on the page.
To do so, we first need to tell django where our templates will be stored. Open in a text editor, and look for the TEMPLATE_DIRS tuple. Enter a full path to an arbitrary place on your filesystem where you will put templates, similar to this:


Then, in this directory, create a file called list_results.html. Enter the following code:

{% for song in songs %}
    {{ song.title }}<br/>
{% endfor %}

You should now be able to start up the server with python runserver, and navigate to http://localhost:8000/list to see your list of two songs.
That’s it for now! Next time, we’ll work on some search functionality including form processing.


About buffalobillion

Web Developer, JavaScript Balrog, Java dude, Ruby/Rails enthusiast. Guitar Playa.
This entry was posted in Web Dev. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s