Django 1.10中文文档:第一个应用 part 3

翻译文档传送门

gitbook地址

https://run-noob.gitbooks.io/django-chinese-docs-1-10/content/

Writing your first Django app, part 3

This tutorial begins where Tutorial 2 left off. We’re continuing the Web-poll application and will focus on creating the public interface – “views.”

紧接着教程二,我们继续开发投票这个web应用,并将注意力集中在创建对外访问的“视图”界面上。

Overview
概览

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 blog 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应用中的一“类”网页,它通常有一个特定的函数以及一个特定的模板。例如,在博客应用中,可能有以下视图:

  • 博客首页 —— 显示最新发表的文章链接。
  • 博客“详细”页面 —— 单篇文章的详细页面。
  • 基于年份的归档页面 —— 显示某给定年份里所有月份发表过的博客。
  • 基于月份的归档页面 —— 显示在给定月份中发表过所有文章。
  • 基于日期的归档页面 —— 显示在给定日期中发表过的所有文章链接。
  • 评论 —— 评论某博客

In our poll application, we’ll have the following four views:

  • Question “index” page – displays the latest few questions.
  • Question “detail” page – displays a question text, with no results but with a form to vote.
  • Question “results” page – displays results for a particular question.
  • Vote action – handles voting for a particular choice in a particular question.

在我们的投票应用中,将有以下四个视图:

  • Question首页 —— 显示最新发布的几个Question。
  • Question“详细”页面 —— 显示单个Question的具体内容,有一个投票的表单,但没有投票结果。
  • Question“结果”页面 —— 显示某Question的投票结果。
  • 投票功能 —— 可对Question中某个Choice的进行投票。

In Django, web pages and other content are delivered by views. Each view is represented by a simple Python function (or method, in the case of class-based views). Django will choose a view by examining the URL that’s requested (to be precise, the part of the URL after the domain name).

在Django中,网页的页面和其他内容都是由视图来传递的(视图对WEB请求进行回应)。 每个视图都是由一个简单的Python函数(或者是基于类的视图的方法)。Django通过检查请求的URL(准确地说,是URL里域名之后的那部分)来选择使用哪个视图。

Now in your time on the web you may have come across such beauties as “ME2/Sites/dirmod.asp?sid=&type=gen&mod=Core+Pages&gid=A6CD4967199A42D9B65B1B”. You will be pleased to know that Django allows us much more elegant URL patterns than that.

平日你上网时,可能会遇到像 “ME2/Sites/dirmod.asp?sid=&type=gen&mod=Core+Pages&gid=A6CD4967199A42D9B65B1B”这样优美的URL。 你将会愉快地了解到,Django允许我们使用更加优雅的URL模式。

A URL pattern is simply the general form of a URL - for example: /newsarchive///.

URL模式就是一个URL的通用形式 —— 例如: /newsarchive///。

To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns (described as regular expressions) to views.

Django使用叫做‘URLconfs’的配置来为URL匹配视图。 一个URLconf负责将URL模式(由正则表达式编写)匹配到视图。

This tutorial provides basic instruction in the use of URLconfs, and you can refer to django.urls for more information.

本教程有URLconfs的基本使用方法,你可以在 django.urls看到更详细的信息 。

Writing more views
Now let’s add a few more views to polls/views.py
. These views are slightly different, because they take an argument:

编写更多的视图

现在让我们给polls/views.py添加一些更多的视图。这些视图和之前的略有不同,因为它们另带了一个参数:

polls/views.py

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

Wire these new views into the polls.urls
module by adding the following url() calls:

通过下面的url() 调用将这些新的视图和polls.urls模块关联起来:

polls/urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /polls/
    url(r'^$', views.index, name='index'),
    # ex: /polls/5/
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

Take a look in your browser, at “/polls/34/”. It’ll run the detail()
method and display whatever ID you provide in the URL. Try “/polls/34/results/” and “/polls/34/vote/” too – these will display the placeholder results and voting pages.

看看你的浏览器,输入“/polls/34/”它将运行detail()方法并显示你在URL中提供的ID。 再试一下“/polls/34/results/”和“/polls/34/vote/” —— 它们将显示出对应的结果界面和投票界面。

When somebody requests a page from your website – say, “/polls/34/”, Django will load the mysite.urls 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. After finding the match at ‘^polls/’, it strips off the matching text (“polls/”) and sends the remaining text – “34/” – to the ‘polls.urls’ URLconf for further processing. There it matches r’^(?P[0-9]+)/$’, resulting in a call to the detail() view like so:

当有人请求你的网站的一个页面时 —— 比如“/polls/34/”,根据ROOT_URLCONF 的设置,Django将加载mysite.urls Python 模块。它查找到名为urlpatterns的变量并按顺序匹配其中的正则表达式。当匹配到’^polls/’,它截断了匹配到的字符串(‘polls/’),然后将剩下的字符串– “34/” – 传递到‘polls.urls’这个URLconf进一步处理。在那它匹配到了 r’^(?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值