Writing your first Django app, part 3
编写你的第一个Django程序,第三部分
This tutorial begins where Tutorial 2 left off. We’re continuing the Web-poll application and will focus on creating the public interface – “views.”
本文继续第二部分所讨论的内容。我们会继续开发网络投票程序并深入研究怎样创建公共接口——使用“视图”。
Philosophy
哲理
A view is a “type” of Web page in your Django application that generally serves a specific function and has a specific template. For example, in a weblog application, you might have the following views:
- Blog homepage – displays the latest few entries.
- Entry “detail” page – permalink page for a single entry.
- Year-based archive page – displays all months with entries in the given year.
- Month-based archive page – displays all days with entries in the given month.
- Day-based archive page – displays all entries in the given day.
- Comment action – handles posting comments to a given entry.
在Django程序中的“视图”是一种可以有着独立功能和模板的网页。比如,在一个博客程序中,你可能有以下视图:
l 博客首页——显示最新的日志。
l 日志详细页面——日志的永久链接页面。
l 基于年份的归档页面——显示给定年份所有有日志的月份。
l 基于月份的归档页面——显示给定月份所有有日志的日期。
l 基于日期的归档页面——显示给定日期所有的日志。
l 评论功能——处理日志的评论。
In our poll application, we’ll have the following four views:
- Poll “archive” page – displays the latest few polls.
- Poll “detail” page – displays a poll question, with no results but with a form to vote.
- Poll “results” page – displays results for a particular poll.
- Vote action – handles voting for a particular choice in a particular poll.
在本文的投票程序中,我们有下面的四种视图:
Poll归档页面——显示最新的所有投票。
Poll详细页面——显示一个投票的问题,可以进行投票但是不显示投票结果。
Poll结果页面——显示一个投票的结果。
投票功能——处理投票的选项。
In Django, each view is represented by a simple Python function.
在Django中,每个视图都是一个Python函数。
Design your URLs
设计你的URL
The first step of writing views is to design your URL structure. You do this by creating a Python module, called a URLconf. URLconfs are how Django associates a given URL with given Python code.
编写视图的第一步就是设计你的URL结构。你需要创建一个叫做URLconf的Python模块。URLconf是连接指定的URL和Python代码的纽带。
When a user requests a Django-powered page, the system looks at the ROOT_URLCONF setting, which contains a string in Python dotted syntax. Django loads that module and looks for a module-level variable called urlpatterns, which is a sequence of tuples in the following format:
当用户请求Django页面时,系统会查找ROOT_URLCONF设置,这是个Python模块的字符串。Django会加载这个模块并查找一个叫做urlpatterns的模块变量,这个变量是由遵循下面格式的元组所组成的序列:
(regular expression, Python callback function [, optional dictionary])
Django starts at the first regular expression and makes its way down the list, comparing the requested URL against each regular expression until it finds one that matches.
Django从第一个正则表达式开始进行迭代,将当前的URL与正则式进行比较直到找到匹配成功的纪录。
When it finds a match, Django calls the Python callback function, with an HttpRequest object as the first argument, any "captured" values from the regular expression as keyword arguments, and, optionally, arbitrary keyword arguments from the dictionary (an optional third item in the tuple).
找到匹配的正则表达式,Django会调用对应的Python回调函数,并传入一个HttpRequest对象作为第一参数,任何根据正则式提取出的值将作为关键字参数传入,而optional dictionary中的值也会作为关键字参数传入。
For more on HttpRequest objects, see the Request and response objects. For more details on URLconfs, see the URL dispatcher.
要了解更多HttpRequest,请参考Request and response objects部分。要了解更多URLconfs,请参考URL dispatcher部分。
When you ran python django-admin.py startproject mysite at the beginning of Tutorial 1, it created a default URLconf in mysite/urls.py. It also automatically set your ROOT_URLCONF setting (in settings.py) to point at that file:
在第一部分里,当你运行python django-admin.py startproject mysite时,就已经在mysite/urls.py创建了一个默认的URLconf设置。它会自动将ROOT_URLCONF(在settings.py文件中)设置指向这个文件:
ROOT_URLCONF = 'mysite.urls'
Time for an example. Edit mysite/urls.py so it looks like this:
下面是个例子。编辑mysite/urls.py,修改成如下代码:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^polls/$', 'mysite.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
)
This is worth a review. When somebody requests a page from your Web site -- say, "/polls/23/", Django will load this Python module, because it's pointed to by the ROOT_URLCONF setting. It finds the variable named urlpatterns and traverses the regular expressions in order. When it finds a regular expression that matches -- r'^polls/(?P<poll_id>\d+)/$' -- it loads the associated Python package/module: mysite.polls.views.detail. That corresponds to the function detail() in mysite/polls/views.py. Finally, it calls that detail() function like so:
我们要来审核一下上面的内容。当有人在你的网站上请求一个页面——例如“/polls/23/”,Django会加载mysite.url这个模块,因为ROOT_URLCONF设置已经指向它。在这个模块中,程序会查找一个叫做urlpatterns的变量并遍历其中的正则式。当它找到一个匹配“r'^polls/(?P<poll_id>\d+)/$'”的表达式时,程序会加载对应的模块:mysite.polls.views.detail。这个模块就是mysite/polls/views.py中的函数detail()。最后,程序会像下面这样调用detail()函数:
detail(request=<HttpRequest object>, poll_id='23')
The poll_id='23' part comes from (?P<poll_id>\d+). Using parenthesis around a pattern "captures" the text matched by that pattern and sends it as an argument to the view function; the ?P<poll_id> defines the name that will be used to identify the matched pattern; and \d+ is a regular expression to match a sequence of digits (i.e., a number).
poll_id=’23’这个部分来自(?P<poll_id>\d+)。用括号包围一个正则式会捕获匹配这个正则式的文字并将捕获到的文字作为视图中对应函数的参数传入;?P<poll_id>这个部分会给匹配到的内容赋予一个名称;\d+则是用来匹配一串数字的正则表达式。
Because the URL patterns are regular expressions, there really is no limit on what you can do with them. And there's no need to add URL cruft such as .php -- unless you have a sick sense of humor, in which case you can do something like this:
由于URLconf设置都是正则表达式,所以你可以毫无限制地来使用它们。而且你也没有必要加上多余的URL部分,比如.php这样的扩展名——如果你像下面这样做,那只能说你天赋异禀、骨骼精奇,太异于常人了:
(r'^polls/latest\.php$', 'mysite.polls.views.index'),
But, don't do that. It's silly.
真的,千万别这样,这太雷人了。
Note that these regular expressions do not search GET and POST parameters, or the domain name. For example, in a request to http://www.example.com/myapp/, the URLconf will look for /myapp/. In a request to http://www.example.com/myapp/?page=3, the URLconf will look for /myapp/.
请注意一下,在正则式中并没有查找GET或是POST参数以及域名。例如,在请求http://www.example.com/myapp/时,URLconf会查找/myapp/。在请求http://www.example.com/myapp/?page=3时,URLconf还是查找/myapp/。
If you need help with regular expressions, see Wikipedia's entry and the Python documentation. Also, the O'Reilly book "Mastering Regular Expressions" by Jeffrey Friedl is fantastic.
如果你在正则表达式上有困难,请看看维基百科和Python文档。当然,O’Reilly出版的图书“Mastering Regular Expressions”(Jeffrey Friedl著)也是非常不错的。
Finally, a performance note: these regular expressions are compiled the first time the URLconf module is loaded. They're super fast.
最后,一个有关性能上的提示:这些正则表达式在第一次加载URLconf的时候就进行编译。它们的速度超快。
Write your first view
编写你的第一个视图
Well, we haven't created any views yet -- we just have the URLconf. But let's make sure Django is following the URLconf properly.
现在我们还没创建任何视图——我们只是设置了URLconf而已。但是我们要先确认Django会遵循这些URLconf的设置。
Fire up the Django development Web server:
启动Django开发服务器:
python manage.py runserver
Now go to "http://localhost:8000/polls/" on your domain in your Web browser. You should get a pleasantly-colored error page with the following message:
现在访问http://localhost:8000/polls/,你应该会得到如下的错误信息:
ViewDoesNotExist at /polls/
Tried index in module mysite.polls.views. Error was: 'module'
object has no attribute 'index'
This error happened because you haven't written a function index() in the module mysite/polls/views.py.
产生这个错误的原因是因为你还没有在mysite/polls/views.py编写index()函数。
Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error messages tell you which view Django tried (and failed to find, because you haven't written any views yet).
试试访问“/polls/23/”、“/polls/23/results/”和“/polls/23/vote/”。这些错误信息会告诉你Django会调用哪些视图函数(调用失败的信息,因为你还没有编写任何视图)。
Time to write the first view. Open the file mysite/polls/views.py and put the following Python code in it:
现在开始编写第一个视图。打开mysite/polls/views.py加入下面的Python代码:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
This is the simplest view possible. Go to "/polls/" in your browser, and you should see your text.
Now add the following view. It's slightly different, because it takes an argument (which, remember, is passed in from whatever was captured by the regular expression in the URLconf):
这就是个最简单的视图了。现在在浏览器里输入“/polls/”,就能看到输出的文字。现在加入下面的代码。它有点不一样,因为它带有一个参数(注意,这个参数是根据URLconf的正则表达式所捕获的内容):
def detail(request, poll_id):
return HttpResponse("You're looking at poll %s." % poll_id)
Take a look in your browser, at "/polls/34/". It'll display whatever ID you provide in the URL.
然后在浏览器里输入“/polls/34/”,就能在页面上看到你在URL中输入的ID值。
Write views that actually do something
让视图真正发挥作用
Each view is responsible for doing one of two things: Returning an HttpResponse object containing the content for the requested page, or raising an exception such as Http404. The rest is up to you.
每个视图函数都必须要做以下两件事情中的一件:返回一个包含页面内容的HttpResponse对象,或者抛出一个异常,比如Http404。剩下的就是你自己的工作了。
Your view can read records from a database, or not. It can use a template system such as Django's -- or a third-party Python template system -- or not. It can generate a PDF file, output XML, create a ZIP file on the fly, anything you want, using whatever Python libraries you want.
视图可以从数据库中读取纪录。它可以使用Django的模板系统,也可以使用第三方的Python模板系统。它可以生成PDF文件,输出XML,实时生成ZIP文件等等,使用Python库你可以做任何你想做的事情。
All Django wants is that HttpResponse. Or an exception.
而Django想要的就是个HttpResponse对象,或者是个异常。
Because it's convenient, let's use Django's own database API, which we covered in Tutorial 1. Here's one stab at the index() view, which displays the latest 5 poll questions in the system, separated by commas, according to publication date:
这真的很方便,现在我们用上第一部分提到过的数据库API来做点东西。修改一下index()视图函数,让它显示最新的5个投票问题,每个问题之间用逗号问分割并按照发布时间降序排列:
from mysite.polls.models import Poll
from django.http import HttpResponse
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
output = ', '.join([p.question for p in latest_poll_list])
return HttpResponse(output)
There's a problem here, though: The page's design is hard-coded in the view. If you want to change the way the page looks, you'll have to edit this Python code. So let's use Django's template system to separate the design from Python:
但是这有个问题,页面的设计是在代码中写死的。如果你想改变页面的外观,就必须修改Python代码了。现在我们用Django模版系统将页面设计从Python代码中分离出来:
from django.template import Context, loader
from mysite.polls.models import Poll
from django.http import HttpResponse
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
t = loader.get_template('polls/index.html')
c = Context({
'latest_poll_list': latest_poll_list,
})
return HttpResponse(t.render(c))
That code loads the template called "polls/index.html" and passes it a context. The context is a dictionary mapping template variable names to Python objects.
上面的代码会加载“polls/index.html”模板并传入一个context对象。这个context对象是一个关联了模板变量和Python对象的字典。
Reload the page. Now you'll see an error:
重新加载页面,你会看到一个错误信息:
TemplateDoesNotExist at /polls/
polls/index.html
Ah. There's no template yet. First, create a directory, somewhere on your filesystem, whose contents Django can access. (Django runs as whatever user your server runs.) Don't put them under your document root, though. You probably shouldn't make them public, just for security's sake. Then edit TEMPLATE_DIRS in your settings.py to tell Django where it can find templates -- just as you did in the "Customize the admin look and feel" section of Tutorial 2.
现在还没创建模板呢。先在你的系统上建立个Django能够访问的文件夹。(Django就像你的服务器一样运行。)但是别把模板放在文档根目录下面。为了安全,你不能把它们设置为公共权限。然后修改settings.py中的TEMPLATE_DIRS加入刚才的创建的文件路径,让Django能够找到模板——就如同你在第二部分中所做的一样。
When you've done that, create a directory polls in your template directory. Within that, create a file called index.html. Note that our loader.get_template('polls/index.html') code from above maps to "[template_directory]/polls/index.html" on the filesystem.
上面完成之后,在模板目录下面创建polls目录,然后在该目录下创建index.html文件。注意一下代码里的loader.get_template('polls/index.html')会加载[template_directory]/polls/index.html这个文件。
Put the following code in that template:
在模板文件中加入下面代码:
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Load the page in your Web browser, and you should see a bulleted-list containing the "What's up" poll from Tutorial 1.
在浏览器中重新加载页面,你能看到一组列表中包含“What’s up”这个投票。