Django URL Dispatcher document translation

Source: Internet
Author: User
Tags python list

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:

  1. 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.

  2. Django loads the Python module and looks for the urlpatterns variable. This should be a python list of django.conf.urls.url () instances.

  3. Django runs each URL pattern sequentially and stops when a URL request is matched.

  4. 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:

  5. l a HttpRequest instance

  6. L If a matching regular expression returns an unnamed group, then this match from the regular expression will be provided with a positional parameter.

  7. 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.

  8. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.