Routes 起步

from routes import Mapper
map = Mapper()
map.connect('spch', '/blog', controller='main', action='index')

result = map.match('/blog')
print result
{'action': u'index', 'controller': u'main'}

1.2 行创建一个mapper
3.   行注册一条路由, 路由名称为'spch', 路径为'/blog', controller为main,
      action为index
      可以这样认为,匹配到此条路由的请求交由controller处理,请求预调用的
      函数为index

5. 创建好路由条目后,即可以进行匹配,调用match方法,匹配路径'blog'
6. 输出匹配结果

map.connect(None, "/error/{action}/{id}", controller="error")
result = map.match('/error/index/2')
print result
{'action': u'index', 'controller': u'error', 'id': u'2'}

1.注册了一条无名路由,并且action从匹配路由中获得
  同样,我们可以省掉None
  map.connect("/error/{action}/{id}", controller="error")
  上述语句同样注册了一条无名路由。


Conditions

Conditions用于限制进行路由匹配,比如method

m.connect("/user/list", controller="user", action="list", conditions=dict(method=["GET", "HEAD"]))
只匹配GET,HEAD请求。


Requirements

有时只想匹配数字,或者匹配可选的几个条目

map.connect(R"/blog/{id:\d+}")
map.connect(R"/download/{platform:windows|mac}/{filename}")

\d表示匹配1位数字,\d+表示匹配多位
windows|mac 表示只匹配windows或者mac
可以将上述写成
map.connect("/blog/{id}", requirements={"id": R"\d+"}
map.connect("/download/{platform}/{filename}",
    requirements={"platform": R"windows|mac"})

Format extensions

通过{.format}来指定匹配格式

map.connect('/entries/{id}{.format}')
print map.match('/entries/2')
{'id': u'2', 'format': None}
print map.match('/entries/2.mp3')
{'id': u'2', 'format': u'mp3'}

map.connect('/entries/{id:\d+}{.format:mp3}')
print map.match('/entries/2.mp3')
{'id': u'2', 'format': u'mp3'}
print map.match('/entries/2')
{'id': u'2', 'format': None}
print map.match('/entries/2.mp4')
None

注意:{id:\d+}, 如果没有\d+, print map.match('/entries/2.mp4')将输出 {'id': u'2.mp4', 'format': None}是可以成功的。
有了\d+后,由于没有匹配format,同时\d+要求只匹配数字,所有2.mp4匹配失败


本文地址:http://blog.youkuaiyun.com/spch2008/article/details/9005109

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值