A new Django Project

Start the Django Project

You can create a new Django project with the following command:

$ django-admin.py startproject cookbook

After you’ve run the command you’ll find the following structure:

cookbook
|-- cookbook
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- manage.py

Test the Development Server

After you’ve created the project, you can change to the directory cookbook:

$ cd cookbook

And try out the development server with the following command:

$ python manage.py runserver
Validating models...

0 errors found
November 10, 2013 - 22:21:46
Django version 1.5.5, using settings 'cookbook.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now you can open the “Welcome to Django” site from http://127.0.0.1:8000/. After you’ve opened the site, you can kill the development server with CTRL + C.

Welcome to Django

Configuration

In order to work with the project, you need to configure it. To do that, open the file settings.py in a text editor.

So that you don’t need to enter the project directory several times in the configuration, you can save it in a “constant”. This constant can then be used everywhere where the project directory is required.

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Note

It is important that the constant is defined at the very beginning of the configuration file.

Next the database needs to be configured. We’ll use SQLite, as it’s built into Python.

Configure the existing database connection default as follows by editing the emphasized lines:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': os.path.join(BASE_DIR, 'default.db'),  # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

Next change the timezone and language to suit:

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Europe/Berlin'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

The constant LANGUAGE_CODE configures the language of the Admin inferface which we will use later to English. You can change it to a different language, e.g. use de as LANGUAGE_CODE if you want to use German.

Lastly, the paths to static files and templates must be defined. Add the emphasized lines to the already existing constants. You can find them further below the configuration file:

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, 'static'),
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(BASE_DIR, 'templates'),
)

Now create the directory for static files and templates under directory cookbook:

$ mkdir static templates

Afterwards the directory structure should look as follows:

cookbook
|-- cookbook
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
|-- manage.py
|-- static
`-- templates

Table Of Contents

Previous topic

Install Django

Next topic

The first Django App

This Page

Languages

Support

If you love django-workshop, consider making a small donation on Flattr:

Useful Links