PracticalDjangoProjects Note 3

Source: Internet
Author: User

How does Django URL configuration work? Simply put, it defines a set of URL patterns and specifies how they match the specific code.

ADMONITION: HOW DJANGO URL CONFIGURATION WORKS

A Django URL configuration file, or URLConf, defines a list of URL patterns and indicates how they map to parts of your code. each URL pattern has at least two parts: a regular expression that describes what the URL looks like and either a view (a Python function that can respond to HTTP requests) to map that URL to or an include, which points to a different URLConf module. the ability to include other URLConf modules makes
It easy to define reusable and "pluggable" sets of URLs, which can be dropped into any point in your project's URL hierarchy.

Also, note that regular expressions are quite strict about matching. ordinarily, a web server will be somewhat lax and treat, for example,/admin and/admin/as the same URL, returning the same result either way. but if you specify a regular expression that ends in a slash-as I'm doing here-you must include the slash on the end when you visit that address in your browser, or the pattern will not match and you'll get a "Page not found" error.

 

ADMONITION: ORDER OF URL PATTERNS
When Django is trying to match a URL, it starts at the top of the list of URL patterns and works its way down until it finds a match. this means that it's better to have more specific patterns like the ^ admin/line come first, and more general patterns like the catch-all for flat pages come last; otherwise, something like the catch-all might match a URL before Django gets to the more specific pattern you actually wanted.

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.