Version 31 (modified by rcooke, 2 years ago) (diff)

Remove duplicate "add app" instruction. Add note about varying paths.

Django Cheat Sheet

The django tutorials are quite good. The goal of this cheat sheet is to create a quick start guide so that after reading the tuts over once or twice you have a more handy reference. Things in this guide are done the 'right' way with generic views and templates right off the bat. Please please add to this page.

(It would be great if some of the stuff from this page could later be rolled in to the startproject command and handled automatically - SimonWillison)

Starting a project… startproject AcmeIntranet
cd AcmeIntranet
mkdir templates
mkdir media

  • edit your database settings
  • set path to media dir ( e.g. /home/joe/AcmeIntranet/media )
    • media contains public files such as css and js files
  • set path to template dir ( e.g. /home/joe/AcmeIntranet/templates )
    • templates contains django html templates
    • your applications will likely have their own template dirs. Remember to add those as you create them ( e.g. /home/joe/AcmeIntranet/templates/invoices )
python syncdb

Creating an invoices application…

python startapp invoices

Create your data model

from django.db import models
from django.contrib.auth.models import User

class Ticket (models.Model):
    user = models.ForeignKey (User) 
    case_number = models.IntegerField()
    dollar_amount = models.DecimalField('Cost (in dollars)', max_digits=10, decimal_places=2)

    def __unicode__(self):
            return u"%s" %

see: model api, model examples

add your app to INSTALLED_APPS in


  1. Project and App file structure depend upon Django version, this will affect the file path to your Apps.
  2. Depending upon how you activate Django, and your server setup, you may need to prepend your project name to your App name like AcmeIntranet.invoices.

    # Uncomment the next line to enable the admin:

validate your model and commit to database:

python validate
python sql invoices
python syncdb

optionally register your model with the administrative interface (if you have the admin enabled under INSTALLED_APPS in, so you get a CRUD UI for free:

# In in the invoices directory...

import models
from django.contrib import admin

Note that the lines above, starting import and from, are commented out by default. Uncomment them to enable admin.

Design your urls

in the project root directory, edit

(r'^invoices/', include('AcmeIntranet.invoices.urls')),

in the invoices app directory create

from django.conf.urls.defaults import *
from models import Ticket

info_dict = {
    'queryset': Ticket.objects.all()

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    (r'^new/$', 'django.views.generic.create_update.create_object', { 'model': Ticket } ),
    (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),

see: generic views

Create your templates

First create Acme_base.html in templates

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" xml:lang="en" lang="en">
    <title>{% block title %}My amazing site{% endblock %}</title>

    <div id="content">
        {% block content %}{% endblock %}

Next create ticket_list.html in templates/invoices

{% extends "Acme_base.html" %}

{% block title %} Listing Invoices {% endblock %}

{% block content %}
    {% for invoice in object_list %}
        <p>{{ invoice.dollar_amount }}</p>
    {% endfor %}
{% endblock %}

See also:

and if you are a mindmap fan:

see: template authoring guide

Back to Top