在suse11.2上使用django1.4.1搞了N久都没有完成这个使命,最后在window下搞定,所以环境,版本很重要:
windows7 + python2.6 + django1.3.3 + sqlite32.4.1
Step1 >>> 建立 project
运行: django-admin.py startproject hello
修改 demo/settings.py 用 sqlite3 作为数据库,产生一 hello.sqlite3 的数据库保存数据,再设定 template 目录等:
import os
PROJECT_DIR=os.path.dirname(__file__)
DATABASES = {
'default': {
'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'hello.sqlite3', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
...
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'static')
...
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR, 'template'),
)
)INSTALLED_APPS = ( ...
'django.contrib.admin',
'hello.blog', # 这个稍后产生,其实就是在hello目录下新建app
)
Step2 >>> demo 目录下建 blog 的 app:
运行:python manage.py startapp blog
修改 ./blog/models.py :
from django.db import models
from django.contrib import admin
# 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
admin.site.register(Category)
admin.site.register(Article)
备注:生成的blog和settings.py 同级目录,这一点差点搞死笔者了,囧...
Step3 >>> 建立数据库
运行:python manage.py sql blog
运行:python manage.py syncdb
后者会建立管理者帐号,设置密码等
修改:hello/urls.py (去掉某些注释增加一个url映射)
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()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'hello.views.home', name='home'),
# url(r'^hello/', include('hello.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)),
(r'^blog/', include('hello.blog.urls')),
)
先在 ./blog/ 下建立一 urls.py
运行 python manage.py runserver,浏览器打开 http://localhost:8000/admin/ 就有可用的前台了!
Step4 >>> 建模版
hello下建文件和目录 template/blog/article_detail.html 和 template/blog/article_list.html
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>对不起没有文章喔!</p>
{% endif %}
article_detail.html
<div class="article">
<div class="title">标题: {{ object.title }}</div>
<div class="pub_date">{{ object.published_at }}</div>
<div class="content">{{ object.content }}</div>
<div class="category">发表于: {{ object.category.name }}</div>
</div>
<p><a href="/admin/blog/article/{{ object.id }}">修改</a></p>
<p><a href="/blog">BACK</a></p>
备注:两个html文件需要使用utf8编码格式保存,否则出现 UnicodeDecodeError
再次运行 python manage.py runserver
笔者的目录结构如下,仅供参考:
文件夹 PATH 列表
卷序列号为 00000002 0C01:3598
D:\DJANGO_PROJ\HELLO
│ hello.sqlite3
│ manage.py
│ settings.py
│ settings.pyc
│ start.bat
│ start.err.log
│ start.run.log
│ urls.py
│ urls.pyc
│ __init__.py
│ __init__.pyc
│ 启动.bat
│
├─blog
│ models.py
│ models.pyc
│ tests.py
│ urls.py
│ urls.pyc
│ views.py
│ __init__.py
│ __init__.pyc
│
├─static
└─template
└─blog
article_detail.html
article_list.html
参考文献:
http://n23.appspot.com/blog/post/3412 【原文】
http://www.doc88.com/p-004801243893.html 【有截图】
http://doc.open-open.com/view/91a54afc2edb4bcfb05fbb275e308cda 【最给力】