Django treats any web site as a project. In Django terms a project is a set of web applications and project- (or site-) specific configuration. You can reuse the same applications in different sites just by deploying them in new projects, and they will automatically use new settings, such as database credentials. A project may contain any number of applications. The term project may sound a bit confusing; I find site or web site more appropriate.
Creating a new project is simple. Assuming you have installed Django correctly, you just need to run the command django-admin.py in the directory where you want the new project directory to be created. Django's administration tool will create a simple project skeleton with basic configuration files. We will use /var/app/vhosts/www_example_com/ as the base directory for the project that will hold all Django applications:
$ django-admin.py startproject www_example_com $ ls -l www_example_com total 12
-rw-r--r-- 1 root root 0 2009-08-24 14:36 __init__.py
-rwxr-xr-x 1 root root 546 2009-08-24 14:36 manage.py -rw-r--r-- 1 root root 2809 2009-08-24 14:36 settings.py -rw-r--r-- 1 root root 578 2009-08-24 14:36 urls.py
In the project directory you'll find the following files:
manage.py: An automatically generated script that you will use to manage you project. Creating new database tables, validating modes or dumping SQL script all is done using this tool. This tool also allows you to invoke a command prompt interface for accessing data models.
settings. py: A configuration file that holds database information as well as application-specific settings.
urls.py: A configuration file that acts as a URL dispatcher. Here you define which views should respond to which URLs.
■Note The configuration file location is specific to your project. In this chapter our project is created in /var/app/virtuai/www_exampie_com/, so assume this location when you see references to the manage.py, settings.py and urls.py files.
Once you have created a new project, you need to specify the database engine that Django should use. As mentioned earlier, we are going to use SQLite. To enable this, we need to make two changes in the settings.py configuration file (referenced as the settings file later in the chapter): specify the database engine and the absolute filename for the database file:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/var/app/virtual/www_example_com/database.db'
When project and database configuration are finished, you can create your application by issuing the following command in your project directory:
$ python manage.py startapp ip_addresses $ ls -l ip_addresses/ total 12
-rw-r--r-- 1 root root 0 2009-08-24 14:55 __init__.py
-rw-r--r-- 1 root root 57 2009-08-24 14:55 models.py
-rw-r--r-- 1 root root 514 2009-08-24 14:55 tests.py -rw-r--r-- 1 root root 26 2009-08-24 14:55 views.py
Just like the Django administration tool, the project management script creates a skeleton for your new application. Now that you have your project (or web site) set up and one application configured, what you need to do is define the data model, write view methods, create the URL structure, and finally design the templates. All that I will describe in more detail in the following sections, but first I still need to show you how to make your new site available for others to see.
The application will not be available for immediate use; you need to provision it in the settings file by appending it to the INSTALLED_APPS list:
INSTALLED_APPS = (
'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'www_example_com.ip_addresses',
Was this article helpful?