URL allocator (URL dispatcher)
A clean, elegant URL scheme is a very important detail in designing high quality Web applications, Django [' dɡo] (pronounced like ' sticky dog ', this set of frames is a Belgian gypsy jazz guitarist Django Reinhardt to name, no other meaning) lets you design URLs in any way you want and without frame structure restrictions. There is no need to use. php or. CGI, and there must be no such absurd thing as0,2097,1-1-1928,00.
Please refer to an article from the founder Tim berners-lee of www:Cool urisdon ' tchange, in this article, about why URLs should be clean and usable. He gave an excellent reason.
Overview
in order to give an application designURLs, you casually created a callURLconfof thePythonmodules, i.e.URLconfiguration, this module is used purelyPythoncode, and it's written inURLpatterns (simple regular expressions) andPythonMethod (Your views) made a simple mapping between them.
This mapping can be long and short in need, and it can refer to other mappings because it is purely Python code, which can be built dynamically.
Django provides a way to follow the active language (not yet understood here, this should and python internationalization related) translation URLs for details , see: internationalizationdocumentation.
How Django handles a request
When a user requests a page from your Django site,
The system determines which Python code should be executed according to the following arithmetic rules:
Django determines the urlconf module in the root directory and uses, typically, setting the value of root_urlconf, but if the HttpRequest object with the Urlconf attribute (set by the middleware) comes in, then its value is replaced with Root_ The settings for the urlconf.
Django loads the Python module and looks for the urlpatterns variable. This should be a python list of django.conf.urls.url () instances.
Django runs each URL pattern sequentially and stops when a URL request is matched.
Once the regular expression is matched, Django imports and invokes the given view, which is the code for a simple Python method, or a class-based view that passes the following parameters:
l a HttpRequest instance
L If a matching regular expression returns an unnamed group, then this match from the regular expression will be provided with a positional parameter.
The key of the L parameter is also made up of any namespaces that are matched by the regular expression, by overriding any parameters specified by the optional arguments of the Kwargs to Django.conf.urls.url (). The value is passed.
If there is no regular expression match, or if an exception is thrown at any program, Django callbacks an appropriate error handling view, see "Exception handling" below for details.
Example
Take a look at the Urlconf sample below
Fromdjango.conf.urlsimport urlfrom.import viewsurlpatterns = [url (r ' ^articles/2003/$ ', views.special_case_2003), ur L (R ' ^articles/([0-9]{4})/$ ', views.year_archive), url (r ' ^articles/([0-9]{4})/([0-9]{2})/$ ', views.month_archive), URL (r ' ^articles/([0-9]{4})/([0-9]{2})/([0-9]+]/$ ', views.article_detail),]
Attention:
When capturing a value from a URL, expand it with parentheses.
There is no need to add a leading slash here, because each URL has, for example, ^articles should not be written ^/articles
this is in front of the regular expression string. ' R ' is optional, but recommended for use on. It tells Python that the string is native and that nothing in this string needs to be escaped. See Dive into Python's explanationfor details.
This article is from "Turing Manor" blog, please be sure to keep this source http://thinkcoding.blog.51cto.com/4410442/1912642
Django URL Dispatcher document translation