« OSS Watch : Slides | Main | Things to go to »

Getting started with Django on Windows

A short (?) story about how I got Django working on my Windows 2000 laptop.

I wanted to get Django working quickly and easily so that I could, if in future necessary, work on a Django project locally rather than edit files remotely on a Linux server. Working locally has certain limitations but is sometimes a god send.

I used the Python 2.4.3 Windows installer and the Django 0.9.1 tar ball, the latter being installed by unpacking it (you can use something like WinRAR for this) then invoking setup.py install. Since I'm just testing if I can get going with the basics of Django on my laptop I'll stick with the built-in webserver for now and worry about Apache later.

Installing Python & Django

Neither python.exe nor django-admin.py are in my path. Searching for django-admin.py turned up a wrapper to it in the Scripts subdirectory of my Python installation. I started a new project like so (in a command shell, start menu -> run -> type cmd and press OK):

c:\ximon\installed_apps\python_2.4.3\python.exe c:\ximon\installed_apps\python_2.4.3\Scripts\django-admin.py startproject tut1
Optional:

What a mouthful. As Valter says on the Installation Guide page of the Django Website you can simplify this:

One trick to make life easier on Windows:

- append ".py" to PATHEXT (Settings - Control Panel - System - Advanced - Environment...)
- append C:\Program Files\Python\Scripts to PATH

This way you can run django-admin.py by just typing "django-admin".

That wasn't enough on my Windows 2000 system, I also had to change the file association (an editor had it) to python.exe (right click on django-admin.py, properties, opens with... change, python). Then the complex command above becomes as simple as:

django-admin startproject tut1

Setting up a database

I chose not to use MySQL since that requires the .NET framework in order to build and install MySQL for Python. Instead I chose SQL Lite since pysqlite has Windows precompiled downloads for Python 2.4 available. I used pysqlite 2.2.2 and SQL Lite 3.3.5.

I edited settings.py in my tut1 project directory (the one created by running django-admin startproject) so that I had:

DATABASE_ENGINE = 'sqlite3'

Starting Django is then just a case of doing:

cd \ximon\data\django_apps\tut1

manage runserver

Tada! At http://127.0.0.1:8000/ you should now have a running Django application, albeit absolutely minimal.

Writing your first Django app, part 1

Now I'm following the tutorial on the Django website. First, edit settings.py to tell Django about my database.All I have to do for sqlite is tell it my DATABASE_NAME which I set to the full path to where I want sqlite to store my database files (c:\ximon\data\sqlite_dbs\tut, but be warned: Django wants forward slashes, not backslashes!) which is the same as the name as my project. You don't have to name them the same thing. Make sure the path to the file exists (in my case I had to create the sqlite_dbs directory since it didn't exist yet). Then tell Django to initialise:

cd \ximon\data\django_apps\tut1

manage init

I continued with the steps of the Django tutorial. The tutorial uses the project name myproject whereas I am using tut1 and so my INSTALLED_APPS entry in settings.py looks like this:

INSTALLED_APPS = ( 'tut1.polls', )

Other than that I completed tutorial one without a hitch. Django running on Windows, tick. Next!

Steps to starting a Django application

django-admin startproject PROJNAME

cd PROJNAME

edit settings.py
- set DATABASE_ENGINE
- set and DATABASE_NAME
- add 'django.contrib.admin' to INSTALLED_APPS

edit urls.py
- uncomment the line after # Uncomment this for admin:

mkdir PATH_TO_DATABASE_FILES (for sqlite)

manage init

manage install admin

manage createsuperuser

manage startapp APPNAME

manage install MODEL

manage runserver

Post a comment