原文在此:https://code.djangoproject.com/wiki/DjangoCheatSheet
Django速查表
Django教程已经非常好了。这个速查表的作用是创建一个快速开始指南,以便在读过一两遍教程之后能有一个更好的手册。
django-admin.py
startproject AcmeIntranet
cd AcmeIntranet
mkdir
templates
mkdir media
settings.py
python manage.py syncdb
2.创建一个发票应用…
python manage.py startapp
invoices
添加”AcmeIntranet.invoices”到settings.py文件中的INSTALLED_APPS。
创建数据模型
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
参见: model
api, model
examples
添加应用到settings.py中的INSTALLED_APPS.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‘,
‘invoices‘,
)
验证模型并提交到数据库:
python
manage.py
validate
python manage.py
sql invoices
python manage.py
syncdb
如果你在settings.py的INSTALLED_APPS中开启了admin,那你也可以注册模型到管理界面,这样你就有了一个免费的CRUD的界面了。
#
In admin.py in the invoices
directory...
import models
from
django.contrib import
admin
admin.site.register(models.Ticket)
注意:上面的行从import和from开始,默认是被注释掉的。去掉注释就能启用admin了。
设计你的urls
在项目的根文件夹,编辑urls.py:
(r‘^invoices/‘,
include(‘AcmeIntranet.invoices.urls‘)),
在发票应用中创建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‘^(?P<object_id>\d+)/$‘,
‘django.views.generic.list_detail.object_detail‘,
info_dict),
)
参见: generic
views
创建你的模板
首先在templates文件夹中创建Acme_base.html
<!DOCTYPE
html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en"
lang="en">
<head>
<title>{% block title %}My amazing site{% endblock
%}</title>
</head>
<body>
<div
id="content">
{%
block content %}{% endblock %}
</div>
</body>
</html>
然后在templates/invoices文件夹中创建ticket_list.html
{%
extends "Acme_base.html" %}
{% block title %} Listing
Invoices {% endblock %}
{% block content
%}
{% for invoice in object_list
%}
<p>{{
invoice.dollar_amount }}</p>
{% endfor
%}
{% endblock
%}
请参阅:
http://www.revsys.com/django/cheatsheet/
http://www.mercurytide.co.uk/news/article/django-cheat-sheet/
http://code.djangoproject.com/wiki/TemplateTagsCheatSheet
如果你是一个思维导图粉: http://code.djangoproject.com/wiki/MindmapCheatsheetForGenericViewsAPI
参见: template
authoring guide
原文:http://www.cnblogs.com/liulixiang/p/3581199.html