Django路由系统介绍
1.一对一路由系统
1.1 路由规则:
url(r'^test/', views.test),
后端对应的处理函数:
def test(request):
return HttpResponse("hello")
访问时可用的url:http://127.0.0.1:8000/test/ 或者http://127.0.0.1:8000/test
浏览器默认会在末尾添加一个“/”
1.1.1 通过get方式传参数:
url(r'^test/?a=111&b=222',views.test),
后端接收参数:
def test(request):
arg1 = request.GET.get("a")
arg2 = request.GET.get("b")
print(arg1,arg2)
return HttpResponse("hello")
1.2 动态路由规则:(通过get的方式传参网站的seo值会比较低)
seo:Search Engine Optimization,通俗理解即为在搜索引擎的排名会较低
静态页面(xxxx.html)和"http://domain name/p1/p2/p3这种格式的网址seo值比较高
1.2.1 除了通过get传递参数,还有以下方法可以传递参数:
url(r'^test/\w+/\w+',views.test),
后端处理函数:
def test(request,a,b):
arg1 = a
arg2 = b
print(arg1,arg2)
return HttpResponse("hello")
访问方式:http://127.0.0.1/test/arg1/arg2
注意:路由系统由上至下匹配,匹配到即终止
urlpatterns = [
url(r'^admin/', admin.site.urls),
# 一对一路由
# 写在此处优先匹配到此记录
url(r'^test/(\w+)/(\w+)/', views.test),
url(r'^test/', views.test),
url(r'^test/?a=111&b=222', views.test),
如果以上两条未注释,通过http://127.0.0.1/test/arg1/arg2访问匹配到了第一条,函数拿不到参数报错
url(r'^test/(\w+)/(\w+)/', views.test),
]
以上视为按位置传参,第一个值一定是传给a,第二个值传给b
还可以按关键字传参:
url(r'^test/(?p<a>\w+)/(?<b>\w+)/', views.test),
后端处理函数a b 形参的为位置可以任意调换位置
def test(request,b,a):
arg1 = a
arg2 = b
print(arg1,arg2)
return HttpResponse("hello")
1.3 url(r'^xxxx',function)第一个参数其实就是正则表达式
通常情况下写完后都回添加“/”
url(r"^test/",views.test) -- 此时只能匹配http://127.0.0.1:8000/test/和http://127.0.0.1:8000/test
url(r"^test",views.test) -- 此时可以匹配http://127.0.0.1:8000/test.*,只要由/test开始,后面可以任意添加
还有另外一种终止符
url(r"^test$",views.test) -- 添加正则终止符
1.4 要想seo值进一步提高,使用伪静态
url(r'^test/(?P<a>\w+)/(?P<b>\w+)/index.html', views.test),
此时通过(http://127.0.0.1:8000/test/arg1/arg2/index.html)访问
2.路由分发--当一个项目下有多个应用时,url容易重复使用
例如:项目下有app01和app02
则可以将项目的urls.py文件拷贝至app下
app01/urls.py
from django.conf.urls import url
from app01 import views
urlpatterns = [
url(r"^test/(?P<a>\w+)/(?P<b>\w+)/index.html$",views.test),
]
app02/urls.py
from django.conf.urls import url
from app02 import views
urlpatterns = [
url(r"^test/(?P<a>\w+)/(?P<b>\w+)/index.html$",views.test),
]
app01处理函数:
def test(request,b,a):
arg1 = a
arg2 = b
print(arg1,arg2)
return HttpResponse("hello,I am app01")
app02处理函数:
def test(request,b,a):
arg1 = a
arg2 = b
print(arg1,arg2)
return HttpResponse("hello,I am app02")
项目下的urls.py
urlpatterns = [
url(r"^app01/",include('app01.urls')),
url(r"^app02/",include('app02.urls')),
]
注:url会分级匹配,先匹配app01/,再到app01/urls.py中继续匹配,app02同理
访问app01:http://127.0.0.1:8000/app01/test/arg1/arg2/index.html
访问app02:http://127.0.0.1:8000/app02/test/arg1/arg2/index.html
3.别名反向生成url:
3.1 给url取别名
url(r"^index.html",views.index,name="u1"),
后端处理函数:
from django.shortcuts import render,HttpResponse
from django.urls import reverse
def index(request):
url = reverse("u1")
return HttpResponse(url)
3.2 反向生成还可以按位置传参:
url(r"^test/(\w+)/index.html$",views.index,name="u2"),
def index(request,a):
print(a)
url = reverse("u2",args=("arg1",))
return HttpResponse(url)
http://127.0.0.1:8000/test/112233/index.html -- 访问方式
后台打印112233,页面返回/test/arg1/index.html
3.3 反向生成之按关键字传参:
url(r"^test/(?P<a>\w+)/index.html$",views.index,name="u2"),
def index(request,a):
print(a)
url = reverse("u2",kwargs={"a":"kwargs"})
return HttpResponse(url)
页面显示:/test/kwargs/index.html
3.4 反向生成url的使用场景:
url(r"^test/(?P<a>\w+)/test.html$",views.test2,name="u3"),
url(r"edit/(\w+)/edit.html$",views.test2_get,name="u4"),
后端处理函数:
def test2(request,a):
test_list = ['manu1','manu2','manu3','manu4']
url = reverse("u3",args=(a,))
print(url,a)
return render(request,'test.html',{"test_list":test_list})
def test2_get(request,a):
name = a
return HttpResponse("正在编辑" + name)
html页面:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>反向生成url应用场景</title>
</head>
<body>
<ol>
{% for i in test_list %}
<li>{{ i }} | <a href="{% url "u4" i %}">编辑</a></li>
{% endfor %}
</ol>
</body>
</html>
通过test匹配的url访问页面,点击页面进入菜单的编辑页面