NewDjangoProject

From XPUB & Lens-Based wiki

Creating a new Django Project from scratch

Note: this recipe is a shrinked, skimmed, compressed and squeezed version of the official Django tutorial. You should check it first and use this recipe as quick reminder.

Prerequisites

This recipes assumes you already have an installed working Apache2 and Django framework.

Start and run a new project

Make a directory that will hold your new Django application and create it.

mkdir ~/Django
cd ~/Django
django-admin.py startproject mysite


This creates a folder called mysite that contains the following files:

  • !__init!__.py: An empty file that tells Python that this directory should be considered a Python package.
  • 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.

To run the development server and see if your django install is correct, do the following:

cd ~/Django/mysite
./manage.py runserver


Then point your browser to http://127.0.0.1:8000 and you should see the message 'It worked! Congratulations on your first Django-powered page'.

Database setup

For most simple and local development it's best to use sqlite. Edit the file '~/Django/mysite/settings.py' and make sure to have the following parameters set:

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/home/your-user-name/Django/mysite/db/mysite.db'


Then, create a folder for your database and sync it:

cd ~/Django/mysite
mkdir db
./manage.py syncdb


You will be asked for an admin username, password and email adress. You will need this information later on.

Create an application

A Django project, such as 'mysite', is a collection of applications. The Django project provides ready made applications that can be used right away to provide special features and components for your project (such as an admin panel). Making a Django project is a combination of ready made generic applications and custom ones that you will write.

Because of this modularity, the application that will write for your project can be used for other projects if needed later on. You can also browse through the many ready to use applications from the Django developers and community.

To create a new application, do the following:

cd ~/Django/mysite
./manage.py startapp myapp


It will create the myapp folder containing basic code to get you started.