当前位置:
文档之家› 中文版django官方教程part2(1)
中文版django官方教程part2(1)
# 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')),
Make the poll app modifiable in the admin 让投票程序的数据可编辑
But where's our poll app? It's not displayed on the admin index page. 但是投票程序显示在哪里?在管理后台的首页上没有显示出来。
You should see a few other types of editable content, including groups, users and sites. These are core features Django ships with by default. 你会看到几块能够进行编辑的内容,包括 Gourps、Users、Sites。这些都是 Django 默认的特性。
生成后台的功能。
Philosophy Generating admin sites for your staff or clients to add, change and delete content is tedious work that doesn’t require much creativity. For that reason, Django entirely automates creation of admin interfaces for models. Django was written in a newsroom environment, with a very clear separation between “content publishers” and the “public” site. Site managers use the system to add news stories, events, sports scores, etc., and that content is displayed on the public site. Django solves the problem of creating a unified interface for site administrators to edit content. The admin isn’t necessarily intended to be used by site visitors; it’s for site managers.
Activate the admin site 启用管理后台
The Django admin site is not activated by default – it’s an opt-in thing. To activate the admin site for your installation, do these three things:
• Edit your mysite/urls.py file and uncomment the lines below the “Uncomment the next two lines...” comment. This file is a URLconf; we’ll dig into URLconfs in the next tutorial. For now, all you need to know is that it maps URL roots to applications. In the end, you should have a urls.py file that looks like this: 默认情况下 Django 管理后台是不启用的——它是可选的。要启用管理后台, 要做三件事:
Writing your first Django app, part 2
编写你的第一个 Django 程序,第二部分
This tutorial begins whereTutorial 1 left off. We’re continuing the Web-poll application and will focus on Django’s automatically-generated admin site. 本文接续第一部分。我们会继续开发网页投票程序,并深入研究 Django 自动
Explore the free admin functionality 体验管理功能
Now that we've registered Poll, Django knows that it should be displayed on the
admin index page: 现在我们在管理后台中注册了 Poll 模型,Django 就知道要在后台首页上显示 出来了:
在 INSTALLED_APPS 设置中加入 django.contrib.admin。 运行 python manage.py syncdb。因为你在 INSTALLED_APPS 中加入了一个新程序,数据表需要更新。
编辑 mysite/urls.py 文件,并将“Uncomment the next two lines...”下面的部分取消注释。这个文件是 URL 配置文件;我们会在后面的部 分深入 URL 配置。现在你所需要知道就是它将 URL 映射到代码中。最后你保 存的 url.py 文件应该向下面这样:
# Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover()
urlpatterns = patterns('', # Example: # (r'^mysite/', include('mysite.foo.urls')),
Enter the admin site 进入管理后台
Now, try logging in. (You created a superuser account in the first part of this tutorial, remember?) You should see the Django admin index page: 现在试试登录进去(还记得不?前面你已经创建了一个超级用户的账户)。你 能看到 Django 管理后台的首页:
Start the development server 启动开发服务器
Let’s start the development server and explore the admin site. 我们来启动开发服务器,看看管理后台是什么样的。
Recall from Tutorial 1 that you start the development server like so: 复习第一部分,启动服务器需要用到下面的命令: python manage.py runserver
哲理 为你的员工或客户创建后台来管理内容是一件不需要什么创意的乏味工作。因为这个原 因,Django 为模型对象有一套完整的自动创建管理界面的机制。 Django 是在一个新闻编辑部里诞生的,在这个环境下在“内容编辑”和“公众网站”之间有 很明显的分界线。网站管理员使用这个系统来增加新闻内容、时间、体育赛事报道等等, 而这些内容会在公众网站上展示出来。Django 为网站管理员提供了一个统一的管理界面。 管理工具不是让网站访问者来使用的;它是为了网站管理员而准备的。
Just one thing to do: We need to tell the admin that Poll objects have an admin interface. To do this, create a file called admin.py in your polls directory, and edit it to look like this: 只需要做一件事:我们要告诉管理后台 Poll 对象要有个管理界面。在 polls 文 件夹下创建一个 admin.py 文件,加入下面的代码: from mysite.polls.models import Poll from django.contrib import admin
Click "Polls." Now you're at the "change list" page for polls. This page displays all the polls in the database and lets you choose one to change it. There's the "What's up?" poll we created in the first tutorial: 点击“Poll”。现在你看到一个投票数据的列表页面了。这个页面显示了数据库 中所有投票数据,你可以点击一个进行修改。刚才我们有个“What’ up”的投票 选项:
Now, open a Web browser and go to "/admin/" on your local domain -e.g.,http://127.0.0.1:8000/admin/. You should see the admin's login screen: 现在打开浏览器,在本地域名上访问/admin/——例如 http://127.0.0.1:8000/admin/。你就会看到下面的登录界面: