In our current project, create a new application called
movies
. At the command line, type:
python manage.py startapp movies
Edit the sigcse/settings.py
file so that we can
connect to a sqlite3 database stored in file called
movies.db
:
DATABASE_ENGINE = 'sqlite3' DATABASE_NAME = 'movies.db' DATABASE_USER = '' DATABASE_PASSWORD = '' DATABASE_HOST = '' DATABASE_PORT = ''
Edit the sigcse/settings.py
file so that we can
use the Django administration site and our application's
database:
INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.admin', 'sigcse.movies', )
Edit the sigcse/urls.py
. Uncomment the highlighted
lines in order to define a URL for accessing the Django
administration site:
from django.conf.urls.defaults import * # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Example: # (r'^sigcse/', include('sigcse.foo.urls')), # Uncomment the admin/doc line below and add 'django.contrib.admindocs' # to INSTALLED_APPS to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: (r'^admin/', include(admin.site.urls)), (r'^hello/(\w+)/$', 'sigcse.hello.views.say'), (r'^fibonacci/(\d{1,4})/$', 'sigcse.fibonacci.views.do_it'), )
Define the application models. Add the following code to the
sigcse/movies/models.py
file:
from django.db import models class Film(models.Model): name = models.CharField(max_length=60) year = models.IntegerField() rating = models.FloatField() def __unicode__(self): return self.name
Register the model that is to be managed using the
administration site. Create a file named admin.py
in the sigcse/movies
folder with the following
content:
from django.contrib import admin from sigcse.movies.models import Film class FilmAdmin(admin.ModelAdmin): # list_display = ('name', 'year', 'rating') # list_filter = ('year',) ordering = ('name',) admin.site.register(Film, FilmAdmin)
Synchronize the model definitions with the database. Type at the command line:
python manage.py syncdb
You'll be asked to create a superuser definition. Type
yes
, and provide all the additional information
requested.
Add new information to the database using the Django shell. Type at the command line:
python manage.py shell
Type the following commands in the interactive shell in order to add two new instances of the Film model:
>>> from sigcse.movies.models import Film >>> f1 = Film(name='Star Wars', year=1977, rating=8.8) >>> f1.save() >>> f2 = Film(name='Avatar', year=2009, rating=8.5) >>> f2.save()
The following commands demonstrate how to retrieve the stored information:
>>> films = Film.objects.all() >>> films [<Film: Star Wars>, <Film: Avatar>] >>> f3 = films[0] >>> f3 <Film: Star Wars> >>> f3.id 1 >>> f3.name u'Star Wars' >>> f3.rating 8.8 >>> f4 = Film.objects.get(name='Avatar') >>> f4.id 2 >>> f4.name u'Avatar' >>> f4.rating 8.5
To populate the database fast, just copy & paste the following instructions into the Python shell:
f = Film(name='Planet of the Apes', year=1968, rating=8.0) f.save() f = Film(name='Alien', year=1979, rating=8.5) f.save() f = Film(name='E.T.: The Extra-Terrestrial', year=1982, rating=7.9) f.save() f = Film(name='Back to the Future', year=1985, rating=8.3) f.save() f = Film(name='Terminator 2: Judgment Day', year=1991, rating=8.5) f.save() f = Film(name='The Matrix', year=1999, rating=8.6) f.save() f = Film(name='Pirates of the Caribbean: The Curse of the Black Python', year=2003, rating=8.0) f.save() f = Film(name='The Lord of the Rings: The Return of the King', year=2003, rating=8.8) f.save() f = Film(name='King Kong', year=2005, rating=7.6) f.save() f = Film(name='V for Vendetta', year=2006, rating=8.1) f.save() f = Film(name='Children of Men', year=2006, rating=8.1) f.save() f = Film(name='Pan\'s Labyrinth', year=2006, rating=8.4) f.save() f = Film(name='WALL-E', year=2008, rating=8.5) f.save() f = Film(name='The Curious Case of Benjamin Button', year=2008, rating=8.0) f.save() f = Film(name='District 9', year=2009, rating=8.3) f.save() f = Film(name='The Book of Eli', year=2010, rating=7.2) f.save()
Open the administration site. Using your web browser, visit this
following URL:
http://localhost:8000/admin/
The administration site allows you to do basic CRUD (Create, Read, Update, Delete) operations. Try several of these operations to see how they work.
Write a view/template in the current application that, given
a certain year (as part of the URL), displays the name of
all the films released that year (using an HTML unordered list).
For example, given the URL
http://localhost:8000/movies/year/2003/
the output
should be:
In the current application, write a view/template that displays
in an HTML table all the films stored in the database, sorted
by rating in descending order. For example, given the URL
http://localhost:8000/movies/ratings/
the output
should look something like this: