创建 admin
应用
打开 guestbook/urls.py 仔细研究一下注释, 会看到有几行写着, 取消注释来激活 admin. django 的 admin 应用是一个非常不错的后台. 现在就来开启它吧from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^guestbook/', include('guestbook.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^$', 'guestbook.home.views.display'),
(r'^echo/$', 'guestbook.home.views.echo'),
(r'^save/', 'guestbook.home.views.save_message'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'guestbook.home',
)
$ guestbook/manage.py syncdb
现在转到 http://localhost:8000/admin/ 会看到登录提示页面啦. 现在填入第一次 syncdb 时输入的帐号密码就能登入了. 不过现在的 admin 还很挫, 甚至不能管理留言. 进入 guestbook/home 目录, 创建 admin.py 源文件, 加入如下内容
from guestbook.home.models import Message
from django.contrib import admin
admin.site.register(Message)