方式一:path
语法:<<转换器类型:自定义>>
作用:若转换器类型匹配到对应类型的数据,则将数据按照关键字传参的方式传递给视图函数
类型:
- str: 匹配除了"/"之外的非空字符串。 /test/zvx
- int: 匹配0或任何整数。 /test/100
- slug: 匹配任意由ASCII字母或数字以及连字符和下划线组成的短标签 例:"detail/<slug:sl>" 匹配:/detail/this-is-django /test/100 /test/zvx /text/100grade
- path: 匹配非空字段,包括路径分隔符"/" 例:"v1/goods/<path:ph>" 匹配: /v1/goods/a/b/c
例子:path("page/int:page",views.xxx)
1)str
url配置
path('test/<str:number>/',test)
view 代码
def test(request,number):
print(number)
return HttpResponse(number)

-
int
url配置
path('test/int:number/',test)
view 代码
def test(request,number):
print(number)
return HttpResponse(number)

3)slug
url配置
path('test/<slug:number>/',test)
view 代码
def test(request,number):
print(number)
return HttpResponse(number)



4)path
url配置
path('test/<path:number>/',test)
view 代码
def test(request,number):
print(number)
return HttpResponse(number)


url配置
path('test/<path:number>',test)
view 代码
def test(request,number):
print(number)
return HttpResponse(number)

url配置
path('child/<str:eid>/<path:oid>/',child),
view代码
def child(request, eid, oid):
print('eid', eid)
print('oid', oid)
return HttpResponse('eid is '+ eid + ' and oid is '+ oid)


url配置
path('child/<str:eid>/<path:oid>',child),
view代码
def child(request, eid, oid):
print('eid', eid)
print('oid', oid)
return HttpResponse('eid is '+ eid + ' and oid is '+ oid)


注意看,后面至少匹配了/
方式二:re_path
在url的匹配过程中可以使用正则表达式进行精准匹配
语法:re_path(reg,view,name=xxx)正则表达式为命名分组模式(?Ppattern);
.+ 表示至少1个值。.* 表示可以有可以为空
url配置
url(r"^child/(?P<eid>.+)/(?P<oid>.*)/$", child)
view代码
def child(request, eid, oid):
print('eid', eid)
print('oid', oid)
return HttpResponse('eid is '+ eid + ' and oid is '+ oid)

