PracticalDjangoProjects 笔记三

Django URL配置是怎么工作的?简单的说,就是定义了一组URL模式并指明是如何与具体的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. 

posted @ 2009-12-16 16:14  小楼  阅读(248)  评论(0编辑  收藏  举报