Roshan Book

My Tech Notebook

Django Commands one project from start to finish


This post is originally published at https://code.djangoproject.com/wiki/DjangoCheatSheet

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…

django-admin.py startproject AcmeIntranet
cd AcmeIntranet
mkdir templates
mkdir media

settings.py

  • 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 manage.py syncdb

Creating an invoices application…

python manage.py startapp invoices

add ‘AcmeIntranet.invoices’, to INSTALLED_APPS list in settings.py

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" % self.id

see:  model api model examples

add your app to INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'AcmeIntranet.invoices',
)

validate your model and commit to database:

python manage.py validate
python manage.py sql invoices
python manage.py syncdb

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

# In admin.py in the invoices directory...

import models
from django.contrib import admin

admin.site.register(models.Ticket)

Design your urls

in the project root directory, edit urls.py:

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

in the invoices app directory create urls.py:

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'^(?Pd+)/$', '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"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">{% block title %}My amazing site{% endblock %}
{% block content %}{% endblock %}

Next create invoice_list.html in templates/invoices

{% extends "Acme_base.html" %}

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

{% block content %}
    {% for invoice in object_list %}

{{ invoice.dollar_amount }}

    {% endfor %}
{% endblock %}
Advertisements

One response to “Django Commands one project from start to finish

  1. nlgnvnli December 13, 2011 at 4:53 am

    dVf65a holvwolcmtat, [url=http://nbgiuuuwinwt.com/]nbgiuuuwinwt[/url], [link=http://brrdajqduykn.com/]brrdajqduykn[/link], http://mlensvogkcnt.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: