Django URL Design Trivia Point

Source: Internet
Author: User

mysite/news/urls.py:

 from  django.conf.urls import   URL  from . import   Viewsurlpatterns  = [URL (r   '  

The above code maps the URLs map to a Python callback function (view) as a simple regular expression. the regular expression "captures" the value in the URLs by means of parentheses.  When a user requests a page, Django will match each pattern in order and stop at the URL of the first matching request. (if there is no match, Django calls a special 404 view.) the entire process is extremely fast because the regular expression is compiled at load time.

once a regular expression is matched, Django will import and invoke the corresponding view, which is actually a simple Python function. Each view will have a request object-it contains the meta-information of the request-and the value captured by the regular expression.

For example, if a user requests the URL "/articles/2005/05/39323/", Django will call the function News.views.article_detail (Request, ' 2005 ',' ', ' 39323 ').

In news.views.py:

def article_detail (request,year, month, number):

......

Call "/articles/2005/05/39323/?day=monday", and the argument is worth the corresponding relationship:

Request. get[' Day '] = = ' Monday '

Year = = 2005

month = = 05

Number = = 39323

Django URL Design Trivia Point

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.