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 models.py file in your text editor. models.py is were your model objects will live. In models.py, 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 settings.py file in the songs directory in a text editor. Find the INSTALLED_APPS section and enter the following line before the closing bracket:

'songs.songster',

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

python manage.py 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 manage.py 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')
>>> song1.save()
>>> song2 = Song(title='Save it for a Rainy Day', artist='Jayhawks')
>>> song2.save()

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 urls.py for your view method
Open up urls.py 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 views.py.

2. Create a list method in views.py that gets the objects from the database.
Open up views.py 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 settings.py 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:

'/home/development/django/templates',

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

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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