python-routes 译 Exmaple

note.refers.link

  • Route 是用 Python 重新实现的 Rails routes system;
  • 它被用来做将 URL 映射为 App 的 action,以及为 application 的 action 产生 URL。

Exmaple

1
2
3
4
5
6
7
8
9
# Setup a mapper
from routes import Mapper
map = Mapper()
map.connect(None, "/error/{action}/{id}", controller="error")
map.connect("home", "/", controller="main", action="index")

# Match a URL, returns a dict or None if no match
result = map.match('/error/myapp/4')
# result == {'controller': 'error', 'action': 'myapp', 'id': '4'}

map.connect

  • 定义映射规则

map.match

  • 获取映射结果

map.resource

  • resource(member_name, collection_name, **kwargs)

参数

  • controller
  • collection
  • new
  • path_prefix
  • name_prefix
  • parent_resource