Finally: an actual Django site!

Setting up the environment

Create your own Python virtual environment, so we aren't all installing things globally on the server and stepping on each others toes. By convention I name my virtual environments with an _env suffix:

$ virtualenv mysite_env

Now activate the virtual environment (and deactivate once, just to see how it works, then activate again):

$ source mysite_env/bin/activate

$ deactivate

$ source mysite_env/bin/activate

Installing pip

Make sure your virtual environment is activated (see the name of your virtual environment in parentheses at the beginning of your shell prompt?) Then install pip into your virtualenv:

$ easy_install pip==dev

Now create an hg repo for your first Django project:

$ hg init mysite

$ cd mysite/

Installing dependencies

We'll keep a list of our dependencies, with the exact versions we need, in a pip requirements file. For now our only dependency is Django itself:

$ emacs requirements.txt

Django==1.1

$ pip install -r requirements.txt

Building the polls app

From this point on, we'll be following the Django tutorial, with some minor changes, as outlined below.

Creating a project

Make sure your virtualenv is activated and you're inside your mysite/ Mercurial working directory when you run django-admin.py startproject mysite. This will result in you having two mysite/ directories, one inside the other; that's fine. The outer one is the container for everything related to your project (for instance, requirements.txt). The inner one is for Django-specific configuration.

The development server

By default, the Django dev server listens for requests on port 8000. Because you're all running the development server on the same server (dj.goshen.edu), you can't all use port 8000. One possibility: everyone use the last four digits of your GC id number as your port number (as long as it's over 1024) - those are likely unique:

python manage.py runserver 0:7887

So to see your work-in-progress, in your browser you'd navigate to:

http://dj.goshen.edu:7887/

Database setup

For now, just set DATABASE_ENGINE='sqlite3'. SQLite is convenient for starting out with; later we'll talk about setting up and using a real database server, such as PostgreSQL or MySQL.

"I'm in part 3 and the admin is giving me ViewDoesNotExist!"

In part 3 of the tutorial, they have you add a number of URLs before you write the associated views. By the end of tutorial 3, you still haven't written the "result" or "vote" views, just "index" and "detail". Normally this isn't a problem as long as you aren't trying to load one of the nonexistent views, but what they don't mention is that it does cause the admin to choke. So you'll get ViewDoesNotExist errors on the admin until early in part 4 when you finally write the "vote" and "results" views. If you want to use the admin, you can fix it sooner by just writing placeholder views that don't do much, similar to the first version of "detail" that you write. This problem is caused by a recent change in the admin code; I've filed a bug with Django about it.

Where should I put my poll template files in part 3?

In part 3 you start writing templates for your poll app: an "index.html" template and a "detail.html" template. The tutorial text seems to suggest that you add a new entry to TEMPLATE_DIRS and place these templates within it. That's kind of silly. You already have a template directory set up for your project from part 2 (it should have an "admin" directory in it with "base_site.html" inside it). There's no reason not to put all of your templates in that directory; so you'd just create a "polls" directory next to the "admin" one and put "index.html" and "detail.html" inside it. No need to add anything new to TEMPLATE_DIRS.