Saturday, June 16, 2007

Begin Django.

Creating a project

run the command startproject mysite. This will create a mysite directory in your current directory.

The development server

Let’s verify this worked. Change into the mysite directory, if you haven’t already, and run the command python runserver.

Changing the port

By default, the runserver command starts the development server on port 8000. If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:

python runserver 8080

Full docs for the development server are at django-admin documentation.

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

Database setup

Now, edit It’s a normal Python module with module-level variables representing Django settings. Change these settings to match your database’s connection parameters:

  • DATABASE_ENGINE — Either ‘postgresql_psycopg2’, ‘mysql’ or ‘sqlite3’. Other backends are also available.
  • DATABASE_NAME — The name of your database, or the full (absolute) path to the database file if you’re using SQLite.
  • DATABASE_USER — Your database username (not used for SQLite).
  • DATABASE_PASSWORD — Your database password (not used for SQLite).
  • DATABASE_HOST — The host your database is on. Leave this as an empty string if your database server is on the same physical machine (not used for SQLite).

sudo aptitude install python-psycopg2

By default, INSTALLED_APPS contains the following apps, all of which come with Django:

  • django.contrib.auth — An authentication system.
  • django.contrib.contenttypes — A framework for content types.
  • django.contrib.sessions — A session framework.
  • django.contrib.sites — A framework for managing multiple sites with one Django installation.

These applications are included by default as a convenience for the common case.

Each of these applications makes use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:

python syncdb

For the minimalists

Like we said above, the default applications are included for the common case, but not everybody needs them. If you don’t need any or all of them, feel free to comment-out or delete the appropriate line(s) from INSTALLED_APPS before running syncdb. The syncdb command will only create tables for apps in INSTALLED_APPS.

Creating models

Projects vs. apps

What’s the difference between a project and an app? An app is a Web application that does something — e.g., a weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.

python startapp polls

Edit the polls/ file so it looks like this:

from django.db import models

class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField('date published')

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(maxlength=200)
votes = models.IntegerField()

  • python sql polls
  • python validate polls — Checks for any errors in the construction of your models.
  • python sqlcustom polls — Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
  • python sqlclear polls — Outputs the necessary DROP TABLE statements for this app, according to which tables already exist in your database (if any).
  • python sqlindexes polls — Outputs the CREATE INDEX statements for this app.
  • python sqlall polls — A combination of all the SQL from the ‘sql’, ‘sqlcustom’, and ‘sqlindexes’ commands.

Now, run syncdb again to create those model tables in your database:

python syncdb

No comments: