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. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值