2.1.5 Design your URLs
A clean,elegant URL scheme is an important detail in a high-quality Web application. Django encourages beautiful URL design and doesn't put any crufy in URLs,like.php or .asp
一个简洁,讲究的URL方案是一个高质量web的关键细节,Django鼓励漂亮的URL设计和不加入任何瑕疵在URL里,想php或asp
To design URLs for an app,you create a Python module called a URLconf . A table of contents for your app, it contains a simple mapping between URL patterns and Python callback functions . URLconfs also serve to decouple URLs from Python code.
为应用设计URLs,你创建一个URLconf模块,一个你app的目录表。它包含简单的映射URL和Python的回调方法的关系。URL配置 用来分离URL和python代码。
Here's what a URLconf might look like for the Reporter/Article example above:
这里展示URl配置和下面记者/文章 的例子:
The code above maps URLs , as simple regular expressions , to the location of Python callback functions("views").The regular expressions use parenthesis to "capture" values from the URLs . When a user request a page,Django runs through each pattern , in order , and stops at the first one that matches the requested URL. (If none of them matches,Django calls a special-case 404 view.) This is blazingly fast ,because the regualr expressions are compiled at load time.
上面的URL映射,非常简单的正则,请求会回调给本地的python方法“views”。正则使用括号从URLs中捕获值,当一个用户需要一个页面,django运行每个模式,然后,停止第一个匹配上的请求URL。(如果没有匹配上,django会调用一个特殊的404页面),这非常快速,因为正在会在加载的时候完成。
Once one of the regexes matches,Django imports and calls the given view ,which is a simple Python function. Each view gets passed a request object - which contains request metadata - and the values captured in the regex.
比对每一个正则时,django会响应一个简单的python方法的视图,每个视图会从正则匹配后的数据中获取元数据和值。
For example ,if a user requested the URL "/articles/2005/05/39323/", Django would call the function new.views.article_detail(request, '2005','05','39323').
例如:如果有 /articles/2005/05/39323/ 的请求,django会传递到方法(request, '2005','05','39323').
2.1.6 Write your views(写视图)
Each view is responsible for doing one of two things: Returning an HttpRespinse object containing the content for the requested page, or raising an exception such as Http404.The rest is up to you
每个视图的责任是做两件事:返回一个包含请求页面的响应对象,或者抛出404页面,接下来取决于你。
Generally, a view retrieves data according to the parameters,loads a template and renders the template with the retrieved data . Here's an example view for year_archive from above:
通常,一个视图通过参数来检索数据,加载一个模板,然后通过检索出的数据渲染模板,这里有一个年的存档视图的例子。
This example uses Django's template system , which has several powerful but strives to stay simple ecough for non-programmer to use
这例子使用了django的模板系统,这是很强大,并且努力去保持足够简单的给非程序员使用。
2.1.7 Design your templates (设计你的模板)
The code above loads the news/year_archive.html template.
上面的例子是加载一个 news/year_archive.html 的模板。
Django has a template search path ,which allows you to minimize redundancy among template. In your Django settings ,you specify a list of directories to check for templates. If a template doesn't exist in the first directory , it checks the second ,and so on。
django有一个模板搜索路径,这回让你在模板中最小的冗余,在django的配置中,你制定一个目录列表来查找模板,如果模板不存在第一个目录下,它会找第二个目录,诸如此类。
Let's say the news/year_archive.html template was found . Here's what that might look like:
当 news/year_archive.html 模板找到后,我们看到的是下面的:
Variable are surrounded by double-curly braces. {{ article.headline }} means "Output the value of the article's headline attribute." But dots aren't used only for attribute lookup : They also can do dictionary-key lookup,index lookup and dunction calls.
变量会被包围在两个大括号中,{{ article.headline }} 的意思是:“输出值为文章的标题属性。”但不能仅适用属性查找,它们也能找字典中的key,索引查找和方法调用
Note {{ article.pub_date|date:"F j, Y" }} uses a Unix-style "pipe"(the "|"character).This is called a template filter ,and it's a way to filter the value of a variable .In this case,the date filter formats a Python datetime object in the given format (as found in PHP's date function; yes ,there is one good idea in PHP).
注意:{{ article.pub_date|date:"F j, Y" }}使用unix的管道(|),这就是模版过滤器,用来过滤变量的值。在这种情况下,日期函数对应python的日期类(想PHP的日期函数,在php是不错的想法)
You can chain together as many filter as you'd like . You can write custom filters . You can write custom template tags,which run custom Pythom code behind the scenes.
你可以整合很多过滤器,可以写自定义的过滤器,可以写自定义的模板标签,在后台可以运营自定义的代码。
Finally,Django uses the concept of "template inheritance":That's what the {% extends "base.html" %} does, It means "First load the template called 'base',which has defined a bunch of blocks ,and fill the blocks with the following blocks." In short ,that lets you dramatically cut down on redundancy in templates:each template has to define only what's unique to that template.
最后,Django使用模板集成的观念:就是 {% extends "base.html" %} ,意思一个加base的模板,定义了一个模块,在接下来的块中填充。总之,让你轻松砍掉模板中的冗余:每个模板会定义独一无二的模板。
Here's what the base.html template might look like :
这里是base.html的大概样子
Simplistically, it defines the look-and-feel of the site(with the site's logo) ,and provides "holes" for child templates to fill, This makes a site redesign as easy as changing a single file -the base template.
简单的说,它定义了网站的外观(加上网站logo),提供空地方来为子模板填充,这会使网站重新设计简单的只改一个文件-base模板。
It also lets you create multiple version of a site ,with different base templates ,while reusing child templates .Django's creators have used this technique to create strikingly different cell-phone editions of sites - simply by creating a new base template.
这回让你通过再利用不同的base模板为网站创建多个版本,Django的创建者使用这个技术创建不同的手机版的网站,只是简单的创建base模板。
Note that you don't have to use Django's template system if you prefer another system. While Django's template system is particularly well-integranted with Django's model layer,nothing forces you to use it .For that matter,you don't have to use Django's database API,either.You can use another database abstraction layer,you can read XML files ,you can read files off disk ,or anything you want .Each piece of Djanog-models,views,templates-is decoupled from the next
注意:如果你更喜欢另外的系统,也可以不使用Django的模板系统。Django的模板系统是和django的模型层特别组装的,不会强迫使用它。关于这一点,你也可以不适用django的数据库API,你可以使用另外的数据库抽象层,你可以读xml,从磁盘上读文件,或者任何你想做的,Django的任何模板,模型,视图,模板彼此间都是不挂钩的。
2.1.8 This is just the surface(这仅仅是皮毛)
This has been only a quick overview of Django's functionality . Some more useful features:
这是django功能的一个简介,很多使用的功能:
A caching framework that integrates with memcached or other back ends.
A syndication framework that makes creating RSS and Atom feeds as easy as writing a small Python class.
More sexy automatically-generated admin features - this overview barely scratched the surface.
一个缓存框架可以结合memcached或者其他后端。
同步框架让订阅RSS或原子feeds至于要写一个小的Python类。
非常好的一个自动后台管理-这个简介仅仅是触及表面。
The next obvious steps are for you to download Django,read the tutorial and join the community . Thanks for your interest!
接下来明显要做的是下载django,阅读手册,然后加入社区,谢谢关注。
2.1节完