阅读:2571次   评论:0条   更新时间:2011-06-01    

Writing your first Django app, part 2

编写你的第一个Django程序,第二部分

 

This tutorial begins where Tutorial 1 left off. We’re continuing the Web-poll application and will focus on Django’s automatically-generated admin site.

本文接续第一部分。我们会继续开发网页投票程序,并深入研究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.

哲理

为你的员工或客户创建后台来管理内容是一件不需要什么创意的乏味工作。因为这个原因,Django为模型对象有一套完整的自动创建管理界面的机制。

Django是在一个新闻编辑部里诞生的,在这个环境下在“内容编辑”和“公众网站”之间有很明显的分界线。网站管理员使用这个系统来增加新闻内容、时间、体育赛事报道等等,而这些内容会在公众网站上展示出来。Django为网站管理员提供了一个统一的管理界面。

管理工具不是让网站访问者来使用的;它是为了网站管理员而准备的。

 

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:

  • Add "django.contrib.admin" to your INSTALLED_APPS setting.
  • Run python manage.py syncdb. Since you have added a new application to INSTALLED_APPS, the database tables need to be updated.
  • 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管理后台是不启用的——它是可选的。要启用管理后台,要做三件事:

l         INSTALLED_APPS设置中加入django.contrib.admin

l         运行python manage.py syncdb。因为你在INSTALLED_APPS中加入了一个新程序,数据表需要更新。

l         编辑mysite/urls.py文件,并将“Uncomment the next two lines...”下面的部分取消注释。这个文件是URL配置文件;我们会在后面的部分深入URL配置。现在你所需要知道就是它将URL映射到代码中。最后你保存的url.py文件应该向下面这样:

 

Changed in Django Development version: The method for adding admin urls has changed in Django 1.1.

Django开发版本中有更改:添加管理后台的URL的方法在Django1.1中已经改变。

 

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'^mysite/', include('mysite.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)),

)

 

(The bold lines are the ones that needed to be uncommented.)

粗体的部分就是要取消注释的代码。

 

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

 

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/。你就会看到下面的登录界面:

 

 

 

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管理后台的首页:

 

 

 

You should see a few other types of editable content, including groups, users and sites. These are core features Django ships with by default.

你会看到几块能够进行编辑的内容,包括GourpsUsersSites。这些都是Django默认的特性。

 

Make the poll app modifiable in the admin

让投票程序的数据可编辑

 

But where's our poll app? It's not displayed on the admin index page.

但是投票程序显示在哪里?在管理后台的首页上没有显示出来。

 

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

 

admin.site.register(Poll)

 

You'll need to restart the development server to see your changes. Normally, the server auto-reloads code every time you modify a file, but the action of creating a new file doesn't trigger the auto-reloading logic.

重启服务器后,你就能看到改变。正常情况下,当你修改一个文件的时候,服务器会重新加载代码;但是添加一个文件并不会重新加载。

 

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就知道要在后台首页上显示出来了:

 

 

 

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”的投票选项:

 

 

Click the "What's up?" poll to edit it:

点击它进行编辑:

 


 
 

 

Things to note here:

  • The form is automatically generated from the Poll model.
  • The different model field types (DateTimeField, CharField) correspond to the appropriate HTML input widget. Each type of field knows how to display itself in the Django admin.
  • Each DateTimeField gets free JavaScript shortcuts. Dates get a "Today" shortcut and calendar popup, and times get a "Now" shortcut and a convenient popup that lists commonly entered times.

有几点需要注意:

l         表单是根据Poll模型自动生成的。

l         不同的字段类型(DateTimeFieldCharField),在页面上会有不同HTML元件来展示。在Django管理后台中,每个字段都有自适应的展现方式。

l         每个DateTimeField都有JavaScript快捷方式。日期有“Today”快捷方式和日期控件弹出框,时间有“Now”快捷方式和常用时间选项弹出框。

 

The bottom part of the page gives you a couple of options:

  • Save -- Saves changes and returns to the change-list page for this type of object.
  • Save and continue editing -- Saves changes and reloads the admin page for this object.
  • Save and add another -- Saves changes and loads a new, blank form for this type of object.
  • Delete -- Displays a delete confirmation page.

页面的底部有一些可选操作:

l         Save——保存当前改变,回到列表页面。

l         Save and continue editing——保存当前改变,重新载入当前编辑数据。

l         Save and add another——保存当前改变,加载一个新的空表单。

l         Delete——显示删除确认页面。

 

Change the "Date published" by clicking the "Today" and "Now" shortcuts. Then click "Save and continue editing." Then click "History" in the upper right. You'll see a page listing all changes made to this object via the Django admin, with the timestamp and username of the person who made the change:

点击“Today”和“Now”快捷方式修改“Date published”字段。然后点击“Save and continue editing”。接着点击右上角的“History”,你会看到当前记录所有的修改历史,包括了修改的时间和操作的用户:

 

 

  • 大小: 10.6 KB
  • 大小: 11.6 KB
  • 大小: 14.7 KB
  • 大小: 10.4 KB
  • 大小: 14 KB
  • 大小: 10.3 KB
  • 大小: 22.6 KB
  • 大小: 15.6 KB
  • 大小: 19.6 KB
  • 大小: 22.5 KB
  • 大小: 24.8 KB
  • 大小: 21.7 KB
  • 大小: 10.9 KB
  • 大小: 10.8 KB
  • 大小: 13.2 KB
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics