大神博客,对我的基础只是补充起到很好的帮助。http:://blog.csdn.net/spch2008/article/details/ 表示感谢。
最重要的参考是文档:https://routes.readthedocs.io/en/latest/modules/mapper.html
开始启程!!!
一、开始routes模块之旅
例子1:
直接上代码:
from routes import Mapper
map = Mapper() #生成的是 Mapper()实例对象
print map
print type(map)
map.connect('lixin', '/blog', controller = 'main', action = 'index')
result = map.match('/blog')
print result
输出结果:
结果分析:
(1)、创建一个mapper()路由实例对象, routes模块可以根据url提取响应的参数,如controller、action或者其他用户自定义函数
(2)、connect注册路由信息。路由名称'lixin', 路径是 ‘/blog’, controller为 ‘main’, action为 ‘index’
匹配到此条路由URL的请求:交由controller类处理,请求预调用的函数index。
(3)、创建好路由条目后,即可进行匹配,调用match方法,匹配路径'blog'。
(4)、输出匹配结果。匹配上之后,匹配结果是一个字典,保存的是后续调用的类和类的方法。
假如匹配不上,会输出的是什么? 如果匹配不上的话,就会输出 None。
例子2:
代码:
from routes import Mapper
map = Mapper()
print map
print type(map)
map.connect(None, '/error/{action}/{id}', controller='error')
result = map.match('/error/lixin/200')
print result
map.connect(None, '/error/{action:index|lixin}/{id:\d+}', controller='error')
result = map.match('/error/lixin/200')
print result
输出结果:
(1)、注册无名路由,action可以从匹配路由中获得。
{ }用来指定里面匹配的字段是什么, : 表示的是匹配字段的格式
(2)、我们可以省略掉None,同样可以注册一条无名路由。
二、渐入佳境
(1)、conditions
conditions 用于限制进行路由匹配,比如method.
from routes import Mapper
map = Mapper()
print map
print type(map)
#4.conditions conditions=dict(method=['GET', 'HEAD'])
map.connect('/user/list', controller = 'user', action = 'list', conditions={'method' : ['GET', 'HEAD']})
result = map.match('/user/list')
print result
输出结果:
只匹配GET、HEAD请求。
POST:新建资源 GET:查询资源 PUT:更新资源 DELETE:删除资源 HEAD:验证,包括用户身份和资源的认证
(2)、Requirements
有时只想要匹配数字,或者匹配可选的几个条目。
map.connect(r'/blog/{id:\d+}')
map.connect(r'/download/{platform:windows|linux/{filename}')
\d([0-9])、+(匹配前一个字符一次或多次)、| 正则表达式知识点。
将上述可以改写成:
map.connect(r'/blog/{id}', requirements={'id':r'\d+'})
map.connect(r'/blog/{platfrom}/{filename}', requirements={'platform':r'windows|linux'})
(3)、Format extensions
通过{.format}来指定匹配格式
#5. {.format}
map.connect('/entries/{id}{.format}', controller = 'main', action = 'index')
map.connect('/entries/{id}{.format:mp8}', controller = 'main', action = 'index')
result = map.match('/entries/50.mp3')
print result
三、Routes Resource(这个当时刚开始写的时候不是很清楚,等到看到后面的时候恍然大悟,所以后面专门有一节来将这个resource)
当路由条目过多时,需要一条一条进行注册,过于麻烦,此时可以通过resource route简化
如下所示:
map.connect("messages", "/messages",
controller="messages", action="create",
conditions=dict(method=["POST"]))
map.connect("messages", "/messages",
controller="messages", action="index",
conditions=dict(method=["GET"]))
map.connect("/messages/{id}",
controller="messages", action="update",
conditions=dict(method=["PUT"]))
map.connect("/messages/{id}",
controller="messages", action="delete",
conditions=dict(method=["DELETE"]))
map.connect("edit_message", "/messages/{id}/edit",
controller="messages", action="edit",
conditions=dict(method=["GET"]))
上述路由条目可以使用这一条语句代替:
map.resource("message", "messages")
两个参数,一个指定单数,为member路由名字;一个指定复数,为collection路由名字。
函数原型:
resource(member_name, collection_name, **kwargs)
由上面可得到下面的路由信息表:解释代表的含义
GET /messages => messages.index() => url("messages")
POST /messages => messages.create() => url("messages")
GET /messages/new => messages.new() => url("new_message")
PUT /messages/1 => messages.update(id) => url("message", id=1)
DELETE /messages/1 => messages.delete(id) => url("message", id=1)
GET /messages/1 => messages.show(id) => url("message", id=1)
GET /messages/1/edit => messages.edit(id) => url("edit_message", id=1)
第一行:GET表示请求资源,路由路径为/messages。当匹配到此条路由路径上的时候,将请求交给messages类的方法index来执行。后面的解释类似。(这一块不知道理解的正不正确)
GET:获取资源 POST:新建资源 PUT:更新资源 DELETE:删除资源
一个member路由指定具体的实例,也就是说有id,而一个collection路由,没有指定的实例,即没有给定id。
综上:member路由操作一个单独的实例,而collection操作全体实例。
map.connect 及map.resource均用来建立路由匹配条件(根据别人的程序主要是理解resource的操作是什么)
匹配条件1
map.connect('/images',controller=a,action='search',conditions={'method':['GET']})
curl | 路由匹配结果 (程序中的route match result is) | curl请求得到的结果 |
curl -X GET http://localhost:8088/images | {'action': u'search', 'controller': <__main__.controller object at 0x10c2b10>} | "do search()" |
匹配条件指定了curl的动作为GET ,访问路径为images 对应的action 为search
匹配条件2
map.connect('name',"/{action}/{pid}",controller=a)
curl | 路由匹配结果 (程序中的route match result is) | curl请求得到的结果 |
curl -X GET http://localhost:8088/show/hihi | {'action': u'show', 'controller': <__main__.controller object at 0x2203b10>, 'pid': u'hihi'} | "do show()" |
curl -X POST http://localhost:8088/failfunc/test | {'action': u'failfunc', 'controller': <__main__.controller object at 0x2203b10>, 'pid': u'test'} | "has no action:failfunc" |
匹配条件3
map.resource("message","messages",controller=a) ,map.resource内部定义了默认的匹配条件
第一个参数message为 member_name(资源名),第二个参数messages为collection_name(资源集合名),一般定义资源集合名为资源名的复数,我这里随便取名
collection_name作为访问的路径名,且当没有传入参数controller时,controller=collection_name
map.resource("message","messages",controller=a) 等同于以下匹配条件:
map.connect('/messages',controller=a,action='index',conditions={'method':['GET']})
map.connect('/messages',controller=a,action='create',conditions={'method':['POST']})
map.connect('/messages/{id}',controller=a,action='show',conditions={'method':['GET']})
map.connect('/messages/{id}',controller=a,action='update',conditions={'method':['PUT']})
map.connect('/messages/{id}',controller=a,action='delete',conditions={'method':['DELETE']})
前两条是针对整个资源集合的操作,后三条是针对资源集合中某个固定资源的操作
curl | 路由匹配结果 (程序中的route match result is) | curl请求得到的结果 |
curl -X POST http://localhost:8088/messages | {'action': u'create', 'controller': <__main__.controller object at 0x1dbbb10>} | "do create()" |
curl -X GET http://localhost:8088/messages | {'action': u'index', 'controller': <__main__.controller object at 0x1dbbb10>} | "do index()" |
curl -X GET http://localhost:8088/messages/12 | {'action': u'show', 'controller': <__main__.controller object at 0x1dbbb10>, 'id': u'12'} | "do show()" |
curl -X PUT http://localhost:8088/messages/12 | {'action': u'update', 'controller': <__main__.controller object at 0x1dbbb10>, 'id': u'12'} | "do update()" |
curl -X DELETE http://localhost:8088/messages/12 | {'action': u'delete', 'controller': <__main__.controller object at 0x1dbbb10>, 'id': u'12'} | "do delete()" |
当url传入的id包含'.',会将'.'后的字符窜匹配为format,如输入的id 为 '12.hihi' ,匹配id='12', format='hihi'
匹配条件4
map.resource('message', 'messages',controller=a,
collection={'search':'GET','create_many':'POST'},
member={'update_many':'POST','delete_many':'POST'})
map.resource除了默认的路由条件外,还可以额外的定义‘资源集合的方法’以及‘单个资源的方法’
collection={'search':'GET','create_many':'POST'} 定义了资源集合方法 search,其curl动作为GET,create_many,其curl动作为POST
member={'update_many':'POST','delete_many':'POST'} 定义了单个资源方法 update_many,其curl动作为POST,delete_many,其curl动作为POST
curl | 路由匹配结果 (程序中的route match result is) | curl请求得到的结果 |
curl -X GET http://localhost:8088/messages/search | {'action': u'search', 'controller': <__main__.controller object at 0x253db10>} | do search() |
curl -X POST http://localhost:8088/messages/create_many | {'action': u'create_many', 'controller': <__main__.controller object at 0x253db10>} | do create_many() |
curl -X POST http://localhost:8088/messages/1/update_many | {'action': u'update_many', 'controller': <__main__.controller object at 0x253db10>, 'id': u'1'} | do update_many() |
curl -X POST http://localhost:8088/messages/1/delete_many | {'action': u'delete_many', 'controller': <__main__.controller object at 0x253db10>, 'id': u'1'} | do delete_many() |
匹配条件5
map.resource('message', 'messages',controller=a,path_prefix='/{projectid}',
collection={'list_many':'GET','create_many':'POST'},
member={'update_many':'POST','delete_many':'POST'})
map.resource初始化时还可以指定curl访问路径的前缀路径,如匹配条件3及4没有指定时,默认为collection_name(资源集合名)
指定path_prefix后,路径为path_prefix/collection_name
curl | 路由匹配结果 (程序中的route match result is) | curl请求得到的结果 |
curl -X POST http://localhost:8088/proj1/messages | {'action': u'create', 'projectid': u'proj1', 'controller': <__main__.controller object at 0x1375b10>} | do create() |
curl -X GET http://localhost:8088/proj1/messages/list_many | {'action': u'list_many', 'projectid': u'proj1', 'controller': <__main__.controller object at 0x1375b10>} | do list_many() |
curl -X POST http://localhost:8088/proj1/messages/member_3/update_many | {'action': u'update_many', 'projectid': u'proj1', 'controller': <__main__.controller object at 0x1375b10>, 'id': u'member_3'} | do update_many() |
参考资料:http://routes.readthedocs.org/en/latest/restful.html
这部分就先介绍到这里,后面介绍routes模块的middleware。