安装Django:
下载:https://www.djangoproject.com/download/
解压后运行:python setup.py
install
参考资料:http://www.cnblogs.com/icyfire/archive/2011/10/10/writing_your_first_django_app.html
1. 创建demo工程
python django-admin.py startproject demo
2. 创建app
python manage.py blog
3. 新建model
在/demo/blog下修改models.py文件如下:
from django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=32)
def __unicode__(self):
return self.name
class Admin:
pass
class Article(models.Model):
title = models.CharField(max_length=64)
published_at = models.DateTimeField('date published')
content = models.TextField()
category = models.ForeignKey(Category)
def __unicode__(self):
return self.title
class Admin:
pass
4. 修改demo下settings.py文件:
a) INSTALLED_APPS 中添加 'blog',例如:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'blog',
)
b) TEMPLATE_DIRS 中添加工程所在路径:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"C:/Users/shenbin/My Documents/Aptana Studio 3 Workspace/demo/demo"
)
5. 建立数据库和表:注意,首先需要检查settings.py文件中关于数据库的设置是否正确。
python manage.py syncdb
6. 新建view页面:
article_list.html
{% if object_list %}
{% for article in object_list %}
<div class="article">
<div class="title">
<a href="/blog/{{ article.id }}">{{ article.title }}</a>
</div>
</div>
{% endfor %}
{% else %}
<p>
Sorry, No Article here.
</p>
{% endif %}
article_detail.html
<div class="article">
<div class="title">
Title: {{ object.title }}
</div>
<div class="pub_date">
{{ object.published_at }}
</div>
<div class="content">
{{ object.content }}
</div>
<div class="category">
Published at: {{ object.category.name }}
</div>
</div>
<p>
<a href="/admin/blog/article/{{ object.id }}">Edit</a>
</p>
<p>
<a href="/blog">BACK</a>
</p>
7. 修改urls.py文件如下:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()
from demo.blog.models import Article
#admin.site.register(Article)
#from demo.blog.models import Category
#admin.site.register(Category)
show_list = {
'queryset' : Article.objects.all(),
}
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'demo.views.home', name='home'),
# url(r'^demo/', include('demo.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
#url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', 'django.views.generic.list_detail.object_list', show_list),
url(r'^blog/(?P<id>\d+)/$', 'django.views.generic.list_detail.object_detail', show_list),
)
启动django:
python manage.py runserver 0.0.0.0:8000
访问: http://localhost:8000/blog 即可。
其他资料:
http://simple-is-better.com/news/275