A clean, elegant URL scheme is an important detail in a high-quality Web application. Django lets you design URLs however you want, with no framework limitations.
How Django processes a request
When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute:
当用户请求一个由Djang驱动的网站页面时,系统决定执行哪些python代码的规则如下。
Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of the ROOT_URLCONF setting.(Django决定要使用的根URLconf模块,通常情况下是配置中的ROOT_URLCONF参数决定。但是如果传入的请求对象有urlconf属性(一般由中间件设置),那么这个值将替换ROOT_URLCONF设置)
Django loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.(Django 加载完URLconf模块后便查找urlpatterns变量,一串django.conf.urls.url()对象的实例列表)
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.(Django遍历每一个URL模式,然后再匹配到第一个请求的URL时停止)
Once one of the regexes matches, Django imports and calls the given view, which is a simple Python function (or a class-based view). The view gets passed the following arguments:(当一个正则匹配的时候,Django导入并且调用相应的视图函数,视图获取传入的参数如下)
- An instance of HttpRequest.(一个HttpRequest的实例)
- If the matched regular expression returned no named groups, then the matches from the regular expression are provided as positional arguments.(如果匹配到的正则表达式没有命名组,那么参数将按照位置传递)
- The keyword arguments are made up of any named groups matched by the regular expression, overridden by any arguments specified in the optional kwargs argument to django.conf.urls.url().(关键参数由任何匹配正则的命名组组成,可以被django.conf.urls.url() 中指定的可选性kwargs参数重写)
If no regex matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view. See Error handling below.(如果匹配失败,或者过程中发生异常,Django将启动一个合理的错误处理视图)
Notes(注意):
To capture a value from the URL, just put parenthesis around it.(为了从URL中获取值,只需要一对小括号即可,eg: url(r’^articles/([0-9]{4})/$’, views.year_archive),)
There’s no need to add a leading slash, because every URL has that. For example, it’s ^articles, not ^/articles.(无需为在URL头部添加斜线,因为默认会有)
- The ‘r’ in front of each regular expression string is optional but recommended. It tells Python that a string is “raw” – that nothing in the string should be escaped. (每个表达式前面的 ‘r’ 是可选但是建议书写的,它告诉Python这个字符串是原始的字符串,不需要任何转义)
Named groups
In Python regular expressions, the syntax for named regular-expression groups is (?Ppattern), where name is the name of the group and pattern is some pattern to match.(在Python表达式中,命名的正则表达式组的语法如(?Ppattern),name就是组的名称,pattern就是要匹配的模式)
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.article_detail),
]