一,URLconf配置
1,基本格式
url(正则表达式,views视图函数,参数,别名),]
2,参数说明
二,正则表达式详解
1,基本配置
urlpatterns =<span style="color: #000000"> [
url(r<span style="color: #800000">'<span style="color: #800000">^articles/2003/$<span style="color: #800000">'<span style="color: #000000">,views.special_case_2003),url(r<span style="color: #800000">'<span style="color: #800000">^articles/([0-9]{4})/$<span style="color: #800000">'<span style="color: #000000">,views.year_archive),url(r<span style="color: #800000">'<span style="color: #800000">^articles/([0-9]{4})/([0-9]{2})/$<span style="color: #800000">'<span style="color: #000000">,views.month_archive),url(r<span style="color: #800000">'<span style="color: #800000">^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$<span style="color: #800000">'<span style="color: #000000">,views.article_detail),]
2,注意事项
- 添加一个前导的反斜杠,因为每个URL 都有。例如,应该是^articles 而不是 ^/articles。
- 加上。
3,补充说明
配置文件中默认没有 APPEND_SLASH 这个参数,但 Django 默认这个参数为 APPEND_SLASH = True。 其作用就是自动在网址结尾加'/'。
如果在settings.py中设置了 ,此时我们请求 http://www.example.com/blog 时就会提示找不到页面
三,分组命名匹配
用法中,可以使用分组命名匹配的正则表达式组来捕获URL中的值并以关键字参数形式传递给视图。
502_69@(?Pname
是组的名称,pattern
是要匹配的模式。
urlpatterns =<span style="color: #000000"> [
url(r<span style="color: #800000">'<span style="color: #800000">^articles/2003/$<span style="color: #800000">'<span style="color: #000000">,url(r<span style="color: #800000">'<span style="color: #800000">^articles/(?P
URLconf 更加明晰且不容易产生参数顺序问题的错误,但是有些开发人员则认为分组命名组语法太丑陋、繁琐。
4,URLconf匹配的位置
URLconf 在请求的URL 上查找,将它当做一个普通的Python 字符串。不包括GET和POST参数以及域名。
URLconf 将查找。
URLconf 仍将查找myapp/
。
URLconf 不检查请求的方法。换句话讲,所有的请求方法 —— 同一个URL的POST
、GET
、HEAD
等等 —— 都将路由到相同的函数。
5,捕捉到的参数永远都是字符串
URLconf中捕获的参数都作为一个普通的Python字符串传递给视图,无论正则表达式使用的是什么匹配方式。例如,下面这行URLconf 中:
函数views.year_archive()
中的year
参数永远是一个字符串类型。
6,视图中指定默认值
urlpatterns
=<span style="color: #000000"> [url(r<span style="color: #800000">'<span style="color: #800000">^blog/$<span style="color: #800000">'<span style="color: #000000">,views.page),url(r<span style="color: #800000">'<span style="color: #800000">^blog/page(?P
<span style="color: #008000">#<span style="color: #008000"> views.py中,可以为num指定默认值
<span style="color: #0000ff">def page(request,num=<span style="color: #800000">"<span style="color: #800000">1<span style="color: #800000">"<span style="color: #000000">):
<span style="color: #0000ff">pass
7,include其他的URLconfs
<span style="color: #008000">#
<span style="color: #008000">For example,here’s an excerpt of the URLconf for the Django website itself.<span style="color: #008000"> <span style="color: #008000">It includes a number of other URLconfs:<span style="color: #0000ff">from django.conf.urls <span style="color: #0000ff">import<span style="color: #000000"> include,url
urlpatterns =<span style="color: #000000"> [
url(r<span style="color: #800000">'<span style="color: #800000">^admin/<span style="color: #800000">'<span style="color: #000000">,admin.site.urls),url(r<span style="color: #800000">'<span style="color: #800000">^blog/<span style="color: #800000">',include(<span style="color: #800000">'<span style="color: #800000">blog.urls<span style="color: #800000">')),<span style="color: #008000">#<span style="color: #008000"> 可以包含其他的URLconfs文件
]
四,命名URL和URL的反向解析
生成的内容中(视图中和显示给用户的URL等)或者用于处理服务器端的导航(重定向等)。
错误)或者设计一种与URLconf 毫不相关的专门的URL 生成机制,因为这样容易导致一定程度上产生过期的URL。
自动更新而不用遍历项目的源代码来搜索并替换过期的URL。
获取一个URL 最开始想到的信息是处理它视图的标识(例如名字),查找正确的URL 的其它必要的信息有视图参数的类型(位置参数、关键字参数)和值。
URLconf,然后可以双向使用它:
用法。第二种方式叫做反向解析URL、反向URL 匹配、反向URL 查询或者简单的URL 反查。
代码了,只需要通过名字来调用当前的URL。
函数中可以这样引用:
URLconf:

django.conf.urls <span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important">from . <span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important">import<span style="margin: 0px; padding: 0px; line-height: 1.5 !important"> viewsurlpatterns =<span style="margin: 0px; padding: 0px; line-height: 1.5 !important"> [
<span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important">#<span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important"> ...
url(r<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important">'<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important">^articles/([0-9]{4})/$<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important">',views.year_archive,name=<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important">'<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important">news-year-archive<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important">'<span style="margin: 0px; padding: 0px; line-height: 1.5 !important">),<span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important">#<span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important"> ...
]
<div class="cnblogs_code_toolbar" style="margin: 5px 0px 0px; padding: 0px"><span class="cnblogs_code_copy" style="margin: 0px; padding: 0px 5px 0px 0px; line-height: 1.5 !important"><a style="margin: 0px; padding: 0px; outline: none; border: none !important" title="复制代码">
<img style="margin: 0px; padding: 0px; height: auto; max-width: 700px; border-width: initial !important; border-style: none !important" src="/res/2019/02-25/18/51e409b11aa51c150090697429a953ed.gif" alt="复制代码">