URL配置(URLconf)就像Django 所支撑网站的目录。它的本质是URL与要为该URL调用的视图函数之间的映射表。
你就是以这种方式告诉Django,对于这个URL调用这段代码,对于那个URL调用那段代码。
from django.conf.urls import url
urlpatterns = [
url(正则表达式, views视图函数,参数,别名),
]
以上参数说明如下:
正则表达式:一个正则表达式字符串
views视图函数:一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串
参数:可选的要传递给视图函数的默认参数(字典形式)
别名:一个可选的name参数
注意事项
urlpatterns中的元素按照书写顺序从上往下逐一匹配正则表达式,一旦匹配成功则不再继续。
若要从URL中捕获一个值,只需要在它周围放置一对圆括号(分组匹配)。
不需要添加一个前导的反斜杠,因为每个URL 都有。例如,应该是^articles 而不是 ^/articles。
每个正则表达式前面的’r’ 是可选的但是建议加上。
我们来看下最基本的正则匹配的一个例子
我们在django_dev下的urls.py中去调用app01下的views.py下的方法login
from app01 import views
urlpatterns = [
url(r'^login/\d{4}/\d{1,2}/', views.login),
]
login函数如下:
def login(request):
return HttpResponse("login")
# 分组匹配
urlpatterns = [
url(r'^admin/(\d{4})/(\d{1,2})/', views.admin),
]
函数如下:
def admin(request,*args):
print(args)
return HttpResponse("admin")
('1990', '12')
URLconf匹配的位置
URLconf 在请求的URL 上查找,将它当做一个普通的Python 字符串。不包括GET和POST参数以及域名。
例如,http://www.example.com/myapp/ 请求中,URLconf 将查找myapp/。
在http://www.example.com/myapp/?page=3 请求中,URLconf 仍将查找myapp/。
URLconf 不检查请求的方法。换句话讲,所有的请求方法 —— 同一个URL的POST、GET、HEAD等等 —— 都将路由到相同的函数。
在Python的正则表达式中,分组命名正则表达式组的语法是(?Ppattern),其中name是组的名称,pattern是要匹配的模式。
# 分组命名
urlpatterns = [
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist),
]
函数如下:
def regist(request,year=2019,month=12):
print(year,month)
print(type(year))
return HttpResponse("regist")
args {'year': '1990', 'month': '12'}
[17/Jan/2018 20:52:48] "GET /regist/1990/12/?a=8&b=9 HTTP/1.1" 200 6
1990 12
<class 'str'>
注意:捕获的参数永远都是字符串
url(r'^param/', views.param,{"param":"safly"}),
函数如下:
def param(request,param= None):
print("name",param)
return HttpResponse("param")
print输出如下:
name safly
url(r'^bieming/(?P\d{4})/(?P\d{1,2})/' , views.bieming,name="reverse"),
函数如下:
from django.urls import reverse
def bieming(request,**kwargs):
print(reverse("reverse",args=(2019,19)))
return HttpResponse("bieming")
测试如下:
print输出如下:
/bieming/2019/19/
以上我们只是测试引用了app1下的试图函数,如果我们有很多app应用,那么我们如何去区分呢?
from app02 import urls as app02_urls
from app03 import urls as app03_urls
urlpatterns = [
url(r'^app02/', include(app02_urls)),
url(r'^app03/', include(app03_urls)),
]
我们在app02中urls.py
from django.conf.urls import url,include
from . import views
urlpatterns = [
# 分组命名
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist),
]
我们在app02中views.py
def regist(request,**kwargs):
return HttpResponse("来自app02的ok")
同理在app3中我们看下urls.py
from django.conf.urls import url,include
from . import views
urlpatterns = [
# 分组命名
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist),
]
同理在app3中我们看下views.py
def regist(request,**kwargs):
return HttpResponse("来自app03的ok")
我们还可以如下配置
from app07 import views
urlpatterns = [
url(r'^app07/', include('app07.urls')), # namespace
]
在app07urls.py中进行如下配置:
from django.conf.urls import url,include
from . import views
urlpatterns = [
url(r'^index/', views.regist, name='n1'),
]
视图函数如下:
def regist(request,**kwargs):
url1 = reverse('n1')
print(url1)
return HttpResponse('经销商'+url1)
以上如果有多个app,同名的话会有问题,那么我们看下如何定义namespace
return (urlconf_module, app_name, namespace)
from app07 import views
urlpatterns = [
url(r'^app07/', include('app07.urls',"app","de")), # namespace
from django.conf.urls import url,include
from . import views
urlpatterns = [
url(r'^index/', views.regist, name='n1'),
]
def regist(request,**kwargs):
url1 = reverse('app:n1')
url2 = reverse('de:n1')
print(url1,url2) # /dealer/index/
#
return HttpResponse('经销商'+url1+url2)
url(r'^test/$', views.test, name="delegate"),
url(r'^testt/(?P\d{4})/(?P\d{1,2})/$' , views.testt, name="delegatee"),
def test(request):
return render(request,"test.html",{"year":2008,"month":12})
def testt(request,**kwargs):
return HttpResponse("ok--testt")
html如下:
<body>
<form action="{% url "delegate" %}" method="post">
<p>用户名:<input type="text" name="name">p>
<p>密码:<input type="password" name="pwd">p>
<p><input type="submit" value="提交">p>
form>
<p>{% url "delegate"%} p>
<p>{% url "delegatee" 2018 12%} p>
<p>{% url "delegatee" year month%} p>
body>
django_dev下的urls.py
url(r'^app02/', include(app02_urls,namespace="app01")),
url(r'^app03/', include(app03_urls,namespace="app02")),
url(r'^test/$', views.test, name="delegate"),
test.py函数
def test(request):
return render(request,"test.html",{"year":2008,"month":12})
test.html页面
<body>
<form action="{% url "delegate" %}" method="post">
<p>用户名:<input type="text" name="name">p>
<p>密码:<input type="password" name="pwd">p>
<p><input type="submit" value="提交">p>
form>
<p>{% url "app01:safly" year month%} p>
<p>{% url "app02:safly" year month%} p>
body>
看下app01的urls.py
urlpatterns = [
# 分组命名
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist,name="safly"),
]
看下app02的urls.py
urlpatterns = [
# 分组命名
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist,name="safly"),
]
测试效果如下:
还有另外一种配置方法如下:
url(r'^app02/', include(app02_urls)),
url(r'^app03/', include(app03_urls)),
url(r'^test/$', views.test, name="delegate"),
test.py代码如下
def test(request):
return render(request,"test.html",{"year":2008,"month":12})
app02下的urls.py
from . import views
app_name = 'app02'
urlpatterns = [
# 分组命名
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist,name="safly"),
]
app03下的urls.py
from . import views
app_name = 'app03'
urlpatterns = [
# 分组命名
url(r'^regist/(?P\d{4})/(?P\d{1,2})/' , views.regist,name="safly"),
]
test.html如下
<body>
<form action="{% url "delegate" %}" method="post">
<p>用户名:<input type="text" name="name">p>
<p>密码:<input type="password" name="pwd">p>
<p><input type="submit" value="提交">p>
form>
<p>{% url "app02:safly" year month%} p>
<p>{% url "app03:safly" year month%} p>
body>