The Django Book

You're reading an outdated version of this book; a newer version is available.

Chapter 2: Getting started

Let’s get started, shall we?

Fortunately, installing Django is easy. Because Django runs anywhere Python does, Django can be configured in many ways. We’ve tried to cover the common scenarios for Django installations in this chapter.

Installing Python

Django is written in 100% pure Python code, so you’ll need to install Python on your system. Django requires Python 2.3 or higher.

If you’re on Linux or Mac OS X, you probably already have Python installed. Type python at a command prompt (or in Terminal, in OS X). If you see something like this, then Python is installed:

Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Otherwise, if you see an error such as "command not found", you’ll have to download and install Python. See http://www.python.org/download/ to get started. The installation is fast and easy.

Installing Django

Installing an official release

Most people will want to install the latest official release from http://www.djangoproject.com/download/. Django uses the standard Python distutils installation method, which in Linux land looks like:

  1. Download the tarball, which will be named something like Django-1.0.tar.gz.
  2. tar xzvf Django-*.tar.gz
  3. cd Django-*
  4. sudo python setup.py install

If everything worked, you should be able to import the module django from the Python interactive interpreter.

>>> import django
>>> django.VERSION
(1, 0, ‘official’)

The Python interactive interpreter:

The Python interactive interpreter is a command-line program that lets you write a Python program interactively. To start it, just run the command python at the command line. Throughout this book, we’ll feature example Python code that’s printed as if it’s being entered in the interactive interpreter. The triple greater-than signs (“>>>”) signify a prompt.

Installing Django from Subversion

If you want to work on the bleeding edge, or if you want to contribute code to Django itself, you should install Django from its Subversion repository.

Subversion is a free, open-source revision-control system similar to CVS, and the Django team uses it to manage changes to the Django codebase. At any given time, you can use a Subversion client to grab the very latest Django source code, and, at any given time, you can update your local version of the Django code — known as your “local checkout” — to get the latest changes and improvements made by Django developers.

The latest-and-greatest Django development code is referred to as “the trunk.”

To grab the latest Django trunk:

  1. Make sure you have a Subversion client installed. You can get the software free from http://subversion.tigris.org/ and excellent documentation from http://svnbook.red-bean.com/
  2. Check out the trunk using the command svn co http://code.djangoproject.com/svn/django/trunk django_src
  3. Symlink django_src/django so that django is within your Python site-packages directory, or update your PYTHONPATH to point to it.

When installing from Subversion, you don’t need to run python setup.py install.

Because the Django trunk changes often with bug fixes and feature additions, you’ll probably want to update it every once in a while — or hourly, if you’re really obsessed. To update the code, just run the command svn update from within the django_src directory. When you run that command, Subversion will contact our Web server, see if any code has changed and update your local version of the code with any changes that have been made since you last updated. It’s quite slick.

Setting up a database

Django’s only prerequisite is a working installation of Python. However, this book focuses on one of Django’s sweet spots, which is developing database-backed Web sites — so you’ll need to install a database server of some sort, for storing your data.

If you just want to get started playing with Django, skip ahead to Starting a project, but trust us — you’ll want to install a database eventually. All of the examples in the book assume you’ve got a database set up.

As of version 1.0, Django supports five database engines:

We’re quite fond of PostgreSQL ourselves, for reasons outside the scope of this book, so we mention it first. However, all those engines will work equally well with Django.

SQLite also deserves special notice: It’s an extremely simple in-process database engine that doesn’t require any sort of server set up or configuration. It’s by far the easiest to set up if you just want to play around with Django.

Using Django with PostgreSQL

If you’re using PostgreSQL, you’ll need the psycopg package available from http://initd.org/projects/psycopg1. Make sure you use version 1, not version 2 (which is still in beta).

If you’re using PostgreSQL on Windows, you can find precompiled binaries of psycopg at http://stickpeople.com/projects/python/win-psycopg/.

Using Django with SQLite 3

You’ll need SQLite 3 — not version 2 — and the pysqlite package from http://initd.org/tracker/pysqlite. Make sure you have pysqlite version 2.0.3 or higher.

Using Django with MySQL

Django requires MySQL 4.0 or above; the 3.x versions don’t support transactions, nested procedures, and some other fairly standard SQL statements. You’ll also need the MySQLdb package from http://sourceforge.net/projects/mysql-python.

Using Django with MSSQL

Using Django with Oracle

Using Django without a database

As mentioned above, Django doesn’t actually require a database. If you just want to use it to serve dynamic pages that don’t hit a database, that’s perfectly fine.

With that said, bear in mind that some of the extra tools bundled with Django do require a database, so if you choose not to use a database, you’ll miss out on those features. (We’ll highlight these features throughout this book.)

Starting a project

If this is your first time using Django, you’ll have to take care of some initial setup.

Run the command django-admin.py startproject mysite. That’ll create a mysite directory in your current directory.

Note

django-admin.py should be on your system path if you installed Django via its setup.py utility. If it’s not on your path, you can find it in site-packages/django/bin; consider symlinking to it from some place on your path, such as /usr/local/bin.

A project is a collection of settings for an instance of Django — including database configuration, Django-specific options and application-specific settings. Let’s look at what startproject created:

mysite/
    __init__.py
    manage.py
    settings.py
    urls.py

These files are:

manage.py
A command-line utility that lets you interact with this Django project in various ways.
settings.py
Settings/configuration for this Django project.
urls.py
The URL declarations for this Django project; a “table of contents” of your Django-powered site.

Where should this code live?

If your background is in PHP, you’re probably used to putting code under the Web server’s document root (in a place such as /var/www). With Django, you don’t do that. It’s not a good idea to put any of this Python code within your Web server’s document root, because it risks the possibility that people may be able to view your code over the Web. That’s not good for security.

Put your code in some directory outside of the document root, such as /home/mycode.

The development server

Change into the mysite directory, if you haven’t already, and run the command python manage.py runserver. You’ll see something like this:

Validating models...
0 errors found.

Django version 1.0, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You’ve started the Django development server, a lightweight Web server you can use while developing your site. We’ve included this with Django so you can develop things rapidly, without having to deal with configuring your production Web server (e.g., Apache) until you’re ready for production. This development server watches your code for changes and automatically reloads, helping you make many rapid changes to your project without needing to restart anything.

Although the development server is extremely nice for, well, development, resist the temptation to use this server in anything resembling a production environment. The development server can only handle a single request at a time reliably, and it has not gone through a security audit of any sort. When the time comes to launch your site, see Chapter XXX for information on how to deploy Django.

Changing the host or the port

By default, the runserver command starts the development server on port 8000, listening only for local connections. If you want to change the server’s port, pass it as a command-line argument:

python manage.py runserver 8080

You can also change the IP address that the server listens on. This is especially helpful if you’d like to share a development site with other developers:

python manage.py runserver 0.0.0.0:8080

will make Django listen on any network interface, thus allowing other computers to connect to the development server.

Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Welcome to Django” page, in pleasant, light-blue pastel. It worked!

What’s next?

Now that we’ve got everything installed and the development server running, let’s write some basic code that demonstrates how to serve Web pages using Django.

Copyright 2006 Adrian Holovaty and Jacob Kaplan-Moss.
This work is licensed under the GNU Free Document License.
Hosting graciously provided by media temple
Comments X

Comments are closed on this chapter.

We're no longer accepting comments on this version of this chapter.

Many thanks to all those who commented.

      About this comment system

      This site is using a contextual comment system to help us gather targeted feedback about the book. Instead of commenting on an entire chapter, you can leave comments on any indivdual "block" in the chapter. A "block" with comments looks like this:

      A "block" is a paragraph, list item, code sample, or other small chunk of content. It'll get highlighted when you select it:

      To post a comment on a block, just click in the gutter next to the bit you want to comment on:

      As we edit the book, we'll review everyone's comments and roll them into a future version of the book. We'll mark reviewed comments with a little checkmark:

      Please make sure to leave a full name (and not a nickname or screenname) if you'd like your contributions acknowledged in print.

      Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful YAHOO.ext library. Thanks also to Yahoo for YUI itself.