首页 技术 正文
技术 2022年11月6日
0 收藏 862 点赞 312 浏览 2025 个字

path()

参数列表:

参数1:字符串类型,用来匹配请求路径

参数2:指定路径所对应的视图函数名

参数3:关键字参数 实际用的不多

参数4…

# urls.py
# 创建子应用的路由文件
from django.urls import path
from day2.views import *
urlpatterns = [
# 参数1:字符串类型,用来匹配请求路径
# 参数2:指定路径所对应的视图函数名
path('hello/', day_2_hello),
path('world/', day_2_hello),
# 转换器的写法,用来匹配url中变量
path('abc/<name>', name),
# 限定变量的类型为int类型
path('aaa/<int:money>', money),
# 参数3:关键字参数 实际用的不多
path('kw_test', kw_test, kwargs={'language': 'python', 'teacher': 'coco', 'age': 18}),
# 参数 name参数 : 给路由字符串加了个别名。name='hello_name'叫反向解析,可以得到路由地址
path('name_test', name_test, name='hello_name'), # 组合使用上面的参数
path('all_together/<int:money>/have', all_together, kwargs={'abc': 123}, name='all_together')]
#views.py
from django.http import HttpResponse
# 函数用来接收用户请求进行处理
# request:django封装好的对象,在调用该函数day_2_hello(request对象)
from django.urls import reverse
def day_2_hello(request):
# pass
# 返回浏览器数据:需要使用response对象
return HttpResponse('你好')def name(request,name):
return HttpResponse('浏览器的地址栏name是{}'.format(name))def money(request,money):
return HttpResponse('浏览器的地址栏money是{}'.format(money))def kw_test(request,**kwargs):
return HttpResponse('{}'.format(kwargs))def name_test(request):
# 接收到的不是helloname,而是当前路由地址
return HttpResponse('name_test的路由是:{}'.format(reverse('hello_name')))def all_together(request,money,**kwargs):
print(money,kwargs)
return HttpResponse("测试一下")

re_path()

# urls.py
from django.urls import re_path, pathfrom day3.views import *urlpatterns = [ # re_path('正则模式', 视图函数名,name='路由别名'),
# 正则模式:(?P<name>正则表达式)
re_path('^get_age/(?P<age>\d{1,2}$)', get_age), # path('1.html',goods),
# path('2.html',goods2),
# 需求:根据不同的商品id展示不同的页面
re_path('^(?P<id>\d+)\.html', goods), # 手机 智能手机 1 老人机 ....
# 华为p50 --->10
# 127.0.0.1:8000/day3/1/50.html
# path('1/50.html',goods2),
re_path('^(?P<catid>\d+)/(?P<goodsid>\d+)\.html$', goods2), path('get_request', get_request)
]
#views.py
from django.http import HttpResponse# Create your views here.
def get_age(request,age):
return HttpResponse('get_age函数的age是={}'.format(age))def goods(request,id):
return HttpResponse('商品{}的详情页'.format(id))def goods2(request,catid,goodsid):
return HttpResponse('分类id={},商品id={}'.format(catid,goodsid))

区别:

path()效率比re_path()高,少一步解析路由地址

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918