【问题标题】:How can I create controller routes in Pylons for multi-part IDs?如何在 Pylons 中为多部分 ID 创建控制器路由?
【发布时间】:2011-03-25 21:29:43
【问题描述】:

默认情况下,Pylons 中的 RESTful 控制器支持对象的单部分 ID。这适用于某些类型的对象,但我的域模型有一组具有复合标识符的对象,我也希望能够为这些对象构建好的 URL。

这是当前支持的:

GET /advanis/saas/projects/id: Show a specific item

这就是我想要的:

GET /advanis/saas/projects/client/key: Show a specific item

如何配置我的路由以支持此功能?

【问题讨论】:

  • 您是否正在使用map.resource() 创建您的路线?

标签: python rest routes pylons


【解决方案1】:

您的路线可以根据需要包含任意数量的动态部分。 Read about route parts here.

快速示例:

config/routing.py:

map.connect('specific_key', '/advanis/saas/projects/{client}/{key}', 
            controller='mycontroller',
            action='item')

controllers/mycontroller.py:

def item(self, client, key):
    ...    

【讨论】:

  • :something 已弃用,取而代之的是 {something}
  • 另请注意,路线的动态部分不必位于末尾。在我当前的一个项目中,我使用map.connect('galleries', '/company/{company_id:\d+}/galleries', controller='galleries', action='index') 或更通用的map.connect('/company/{company_id:\d+}/{controller}/{id}/{action}'),因为所有操作都取决于所选公司(客户)。 :\d+ 表示ID为数字(其他ID不匹配)。
猜你喜欢
  • 2012-05-14
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多