Python Django

The docs are good, including the tutorial. If you find the tutorial exhausting, start with Django at a glance.

On Debian: apt-get install python-django sqlite.

Starting a Project

$ django-admin startproject myproject
$ cd myproject
$ tree
└── myproject

1 directory, 5 files

The includes the database info. It used SQLite by default. When using another database (e.g. MySQL), create an empty database at this point (CREATE DATABASE myproject;).

The INSTALLED_APPS tuple includes defaults like 'sessions'. To create the database tables used by these default apps:

$ python migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK

Development server

$ python runserver
Performing system checks...

System check identified no issues (0 silenced).
July 23, 2015 - 21:01:21
Django version 1.7.9, using settings 'myproject.settings'
Starting development server at
Quit the server with CONTROL-C.

Adding an App to the project

Each app is a Python package that follows certain Django conventions.

$ python startapp myapp
--- bava --- myproject % tree myapp
├── migrations
│   └──

1 directory, 6 files 

Creating models

Edit mypreoject/myapp/

from django.db import models

class Note(models.Model):
    rec_date = models.DateTimeField('Date Recorded')
    note_text = models.CharField(max_length=1024)
    importance = models.IntegerField(default=1)
    def __str__(self):
        return self.note_text

class Category(models.Model):
    note = models.ForeignKey(Note);
    category_name = models.CharField(max_length=64)
    def __str__(self):
        return self.category_name

Add 'myapp' to the INSTALLED_APPS tuple in

Run python makemigrations myapp. If desired, we can review the myapp.0001_initial plan prior to actually touching the database schema. When ready to actually touch the database schema:

$ python migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, myapp, auth, sessions
Running migrations:
  Applying myapp.0001_initial... OK

Playing with the API

$ python shell
Python 2.7.10 (default, Jul  1 2015, 10:54:53) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from myapp.models import Note, Category
>>> Note.objects.all()
>>> from django.utils import timezone
>>> n = Note(, note_text="My first note!", importance=2)
>>> Note.objects.all()
[<Note: My first note!>]

Create an admin user

$ python createsuperuser
Username (leave blank to use 'username'): admin
Email address:
Password (again): 
Superuser created successfully.

Add this to myapp/

Run the dev server, and look at


Edit Set DEBUG = False and TEMPLATE_DEBUT = False.

Our project code should be deployed outside the webserver document root.