一、位置分组与关键字分组
位置分组:
-按位置传参
-分组之后,会把分组出来的数据,当位置参数,传到视图函数,所以,视图函数需要定义形参
urls.py
# 精确匹配
url(r'^publish/$', views.publish)
views.py
def publish(request):
if request.method=='GET':
return HttpResponse("This is get")
elif request.method=='POST':
return HttpResponse('This is POST')
# 访问:http://127.0.0.1:8000/publish/
This is get
urls.py
# 匹配publish 后四位数字
url(r'^publish/[0-9]{4}/$', views.publish)
# 匹配任意长度的数字
url(r'^publish/\d+/$',views.publish)
views.py
同上面一样
urls.py
# 配后面的publish/数字4位/与数字2位 ,其他均为404 (注:多个分组中都带有小括号)
url(r'^publish/([0-9]{4})/([0-9]{2})/$',views.publish),
def publish(request,year,month): # 有括号(分组)的必须要传参数过去
if request.method=='GET':
return HttpResponse(' This is get')
elif request.method=='POST':
return HttpResponse('This is POST')
视图函数还可以这样写(*args):
def publish(request,*args):
if request.method == 'GET':
return HttpResponse(' This is get')
elif request.method == 'POST':
return HttpResponse('This is POST')
访问:http://127.0.0.1:8000/publish/1234/20/
This is get
urls.py
# 有括号(分组)的必须要传参数过去,publish后面匹配多个数字
url(r'^publish/(\d+)/$',views.publish),
views.py
def publish(request,year):
return HttpResponse("publish")
关键字分组:
-按关键字传参
-有名分组之后,会把分组出来的数据,当关键字参数,传到视图函数,所以,视图函数需要定义形参,形参名字要跟分组的名字对应,与顺序无关
urls.py
# 关键字分组是按照关键字传参数
url(r'^publish/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$',views.publish),
views.py
def publish(request,year,month): # 有括号(分组)的必须要传参数过去
if request.method=='GET':
return HttpResponse(' This is get')
elif request.method=='POST':
return HttpResponse('This is POST')
urls.py
# 位置分组与关键字分组混合使用
url(r'^publish/([0-9]{4})/(?P<month>[0-9]{2})/$',views.publish),
# 位置分组与关键字分组混合使用,可以使用*args,**kwargs 接收 (建议不要混着用)
def publish(request,*args,**kwargs):
if request.method=='GET':
print(args,kwargs) # () {'month': '12'}
return HttpResponse(' This is get publish')
elif request.method=='POST':
return HttpResponse('This is POST')
二、反向解析
# 分组
作用:
例如:当我访问http://127.0.0.1:8000/publish/ 去点击某一个链接的时候,去访问到了http://127.0.0.1:8000/publishadd/
urls.py
url(r'^publish/$',views.publish),
url(r'^publishadd/$',views.publishadd,name='pub'),
# 若以后要去到这个地址(publishadd)会发生改变的话,后面可以加上‘name=自已定义的名字与pub.html网页标签中定义的名字一致’,这样,publishadd这个路由再如何变化,访问到/publish/中指定某一个链接时,也还是会去到你已改变的那个路由上的。(类似给publishadd,取了一个别名)
def publish(request):
if request.method=='GET':
return render(request, 'pub.html')
def publishadd(request):
return HttpResponse('publishadd')
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
{#位置分组#}
<a href="{% url 'pub'%}">点我去publishadd</a>
</body>
</html>
对于redirect 重定向的解决方案:
没改前:
# 当我访问:http://127.0.0.1:8000/publish,它会帮我跳转到http://127.0.0.1:8000/publishadd/这个页面中,但我路由层的publishadd换了其他名字的时候,便会报错了。
urls.py
url(r'^publish/$',views.publish),
url(r'^publishadd/$',views.publishadd,name='pub'),
def publish(request):
if request.method=='GET':
return redirect('/publishadd/')
def publishadd(request):
return HttpResponse('publishadd')
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
{#位置分组#}
<a href="{% url 'pub'%}">点我去publishadd</a>
{#<a href="{% url 'pub' year=2018 month=12 %}">test去点我</a>#}
</body>
</html>
例如:访问http://127.0.0.1:8000/publish,路由层publishadd改成了publishadd777,它也会访问到publishadd这个函数的属性中
urls.py
url(r'^publish/$',views.publish),
url(r'^publishadd/$',views.publishadd,name='pub'),
from django.shortcuts import render,HttpResponse,redirect,reverse
def publish(request):
if request.method=='GET':
url=reverse('pub') # 改之后,reverse反向解析的模块
return redirect(url) # 改之后
def publishadd(request):
return HttpResponse('publishadd')
# 位置分组,参数传递
urls.py
url(r'^publish/$',views.publish),
url(r'^publishadd/([0-9]{4})/$',views.publishadd,name='pub'), # 如果publishadd后面是两个参数的话,views.py中的也需要跟着加
def publish(request):
if request.method=='GET':
return render(request, 'pub.html')
def publishadd(request,year): # urls.py 中的publishadd后面要加2个参数的话,这里也要跟着加上2个参数,例如:def publishadd(request,year,month):
return HttpResponse('publishadd')
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
{#位置分组#}
<a href="{% url 'pub' 2018 %}">点我去publishadd</a> {# 如果上面是两个参数的话 2018的后面还需要多传一个参数 #}
</body>
</html>
访问:http://127.0.0.1:8000/publish/
点击页面中的 "点我去publishadd"
访问到:http://127.0.0.1:8000/publishadd/2018/
显示:publishadd 成功。
使用reverse模块重定向
urls.py
url(r'^publish/$',views.publish),
url(r'^publishadd/([0-9]{4})/([0-9]{2})/$',views.publishadd,name='pub'),
from django.shortcuts import render,HttpResponse,redirect,reverse
def publish(request):
if request.method=='GET':
url=reverse('pub',args=(2018,12,))
return redirect(url)
def publishadd(request,year,month):
return HttpResponse('publishadd')
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
{#位置分组#}
<a href="{% url 'pub' 2018 02 %}">点我去publishadd</a>
</body>
</html>
访问:http://127.0.0.1:8000/publish
自动跳转到:http://127.0.0.1:8000/publishadd/2018/12/
显示:publishadd 成功。
# 关键字分组 --- 模板层
urls.py
url(r'^publish/$',views.publish),
url(r'^publishadd/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.publishadd, name='pub'),
def publish(request):
if request.method=='GET':
return render(request, 'pub.html')
def publishadd(request,year,month):
return HttpResponse('publishadd')
templates/pub.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
{#关键字分组#}
<a href="{% url 'pub' year=2018 month=12 %}">点我去publishadd</a>
</body>
</html>
访问:http://127.0.0.1:8000/publish/
点击 :点我去publishadd
访问到:http://127.0.0.1:8000/publishadd/2018/12/
显示:publishadd 成功~
# 关键字视图层
urls.py 与 templates/pub.html 内容不变(与上面的一致)
views.py
def publish(request):
if request.method=='GET':
url=reverse('pub',args=(2018,12,))
# url=reverse('pub',kwargs={'month':12,"year":2018}) # 两种都支持,效果一样。
return redirect(url)
def publishadd(request,year,month):
return HttpResponse('publishadd')
访问:http://127.0.0.1:8000/publish
会重定向到:http://127.0.0.1:8000/publishadd/2018/12/
显示 : publishadd !~~~成功
总结:
-先命一个名:
-1 无参数:url(r'^publishadd133/$', views.publishadd,name='ddd'),
-2 位置分组:url(r'^publishadd/([0-9]{4})/([0-9]{2})/$', views.publishadd,name='ddd'),
-3 关键字分组:url(r'^publishadd/(?P<year>[0-9]{4})/(?P<mounth>[0-9]{2})/$', views.publishadd,name='ddd'),
-在模板层:
-1 无参数:{% url 'ddd' %}
-2 位置分组的:{% url 'ddd' 2018 12 %}
-3 关键分组:{% url 'ddd' 2018 12 %} 还可以 {% url 'ddd' year=2018 mounth=12 %}
-在视图层:
from django.shortcuts import reverse
在视图函数里:
1 无参数:url=reverse('ddd')
2 位置分组:url=reverse('ddd',args=(2018,12,)) 如果只有一个参数,后面必须要加一个逗号
3 关键字分组:url=reverse('ddd',args=(2018,12,)) 还可以 url=reverse('ddd',kwargs={'year':2018,'mounth':12})
三、路由分发
作用:由总路由urls.py中先分发,然后再到不同的app中由它们再次分发。
1、首先要再次手动创建一个app
命令:python3 manage.py startapp appname (我这里就叫blog了,另一个为app01)
2、在settings.py中注册
INSTALLED_APPS 的列表中添加
'blog.apps.BlogConfig',
app01目录下的urls.py中:
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^app01_test/$',views.test),
]
app01目录下的视图函数views.py文件中:
def app01(request):
return HttpResponse('app01--test')
blog目录下的urls.py中:
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^blog_test/$',views.test)
]
blog目录下的视图函数views.py文件中:
def blog(request):
return HttpResponse('blog----test')
4、在总路由中的urls.py中的需要配置以下:
from django.conf.urls import url,include
# 总路由中添加:
url(r'^app01/',include('app01.urls')),
url(r'^blog/',include('blog.urls')),
5、访问:
http://127.0.0.1:8000/blog/blog_test/
http://127.0.0.1:8000/app01/app01_test/
总结:
路由分发
1 在不同的app里创建urls.py
2 在总路由
-from django.conf.urls import include
-url(r'^blog/',include('blog.urls')),
-url(r'^app01/',include('app01.urls')),
3 在不同的app的urls里配置路由关系
***重点***总路由,不能加结束符$
四、名称空间
作用:以防止两个应用的子路由后面的name='名字'相同或者怕冲突,这样是为了让它们自已找到属于自已的名称空间,以防冲突(或者不要把子路由后面的name='名字'名字命名成一样的也可。做了解)
url(r'^app01/',include('app01.urls',namespace='app01')),
url(r'^blog/',include('blog.urls',namespace='blog')),
app01 下的文件:
app01下的urls.py
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r'^app01_test/$',views.app01,name='test'),
]
app01下的views.py
def app01(request):
url=reverse('app01:test')
print(url)
return render(request,'app01.html')
templates/app01.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
<a href="{% url 'app01:test' %}">app01_test</a>
</body>
</html>
blog下的文件:
blog下的urls.py
from django.conf.urls import url
from blog import views
urlpatterns = [
url(r'^blog_test/$',views.blog,name='test')
]
def blog(request):
url=reverse('blog:test')
print(url)
return render(request,'blog.html')
templates/blog.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>反向解析</title>
</head>
<body>
<a href="{% url 'blog:test' %}">blog_test</a>
</body>
</html>
五、伪静态
-路由:url(r'^book/(?P<id>\d+.html)',views.book),
-访问:http://127.0.0.1:8000/book/4.html
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。