巫山网站建设,专业建设信息化网站资源,深圳最新政策消息,网站开发与设计中学生routes是用python重新实现的Rails routes系统#xff0c;用于将url映射到应用程序的actions #xff0c;并反过来生成url 它也是在openstack实现restful通信的方式#xff0c;它被用来做将 URL 映射为 App 的 action#xff0c;以及为 App的action 产生 URL
两个重要的方法…routes是用python重新实现的Rails routes系统用于将url映射到应用程序的actions 并反过来生成url 它也是在openstack实现restful通信的方式它被用来做将 URL 映射为 App 的 action以及为 App的action 产生 URL
两个重要的方法map.connect 定义映射规则 和 map.match 获取映射结果
map.connect映射规则(map名称,路由地址,controller,action,conditions,requirements) 除了路由地址其余都是附加参数对映射进行一些处理 controller资源控制类 action:资源处理操作 requirements对输入格式进行限制 conditions路由方式
map.routematch返回结果如果匹配到了会返回一个二元元组元组第一个元素为匹配到的字典第二个元素一个Route对象 map.match:比routematch少返回了一个route对象
# !/usr/bin/env python
# coding: utf-8from routes.mapper import Mapper# 实例化一个Mapper对象
mapper Mapper()# 注册一个/hi路由
mapper.connect(/hi)mapper.connect(None, /error/{action}/{id}, controllererror)
mapper.connect(home, /hi2, controllermain, actionindex)
mapper.connect(/hi2/{action})
mapper.connect(/hi3/{action}/{id})m4_res mapper.routematch(/error/az/a)
print(m4_res)# 查找/hi
m_result mapper.routematch(/hi)
print(m_result)# 查找/hi2
m2_result mapper.routematch(/hi2/h2)
print(m2_result)# 查找/hi3
m3_result mapper.routematch(/hi3/boy/23)
print(m3_result)
执行结果如下
({action: az, id: a, controller: error}, routes.route.Route object at 0x0000020481DF5D90)
({}, routes.route.Route object at 0x0000020481DF5D00)
({action: h2}, routes.route.Route object at 0x0000020481DF5B50)
({action: boy, id: 23}, routes.route.Route object at 0x0000020481DF57C0当映射规则很多的时候需要使用很多次 map.connect这时可以使用 map.resource 方法 map.resource内部定义了默认的匹配条件 第一个参数message为 member_name(资源名第二个参数messages为collection_name资源集合名一般定义资源集合名为资源名的复数 collection_name作为访问的路径名且当没有传入参数controller时controllercollection_name
from routes.mapper import Mapper# 实例化一个Mapper对象
mapper Mapper(always_scanTrue)
mapper.resource(message, messages, controllertestuse)
# 等同于以下匹配条件mapper.connect(/messages, controllertestuse, actionindex, conditions{method: [GET]})mapper.connect(/messages, controllertestuse, actioncreate, conditions{method: [POST]})mapper.connect(/messages/{id:[0-9]}, controllertestuse, actionshow, conditions{method: [GET]})mapper.connect(/messages/{id}, controllertestuse, actionupdate, conditions{method: [PUT]},requirementsdict(idr[a-z]))
mapper.connect(/messages/{id:[XYZ]}, controllertestuse, actiondelete, conditions{method: [DELETE]})