包含金字塔配置时如何使用裸路径(无斜线)?
How to use bare path (no slash) when including pyramid config?
我正在尝试通过 including a callable 包含多个 config.add_route 函数来制作模块化金字塔应用程序。在我的 init.py:
def devices_include(config):
config.add_route("devices.collection", "/")
config.add_route("devices.single", "/{device_id}")
...
def main(global_config, **settings):
...
config.include(devices_include, route_prefix="/devices")
此代码有效,这意味着当我转到 /devices/
时,它 运行 是 devices.collection
函数,当我转到 /devices/1
时,它是 运行 函数devices.single
函数。但是,我希望在转到 /devices
时能够 运行 devices.collection
函数(没有尾部斜线)。我该怎么做?
随时深入探讨 https://github.com/Pylons/pyramid/issues/406 和许多相关问题。简短的回答是,如果你想让它工作,你需要停止使用 route_prefix
或者你可以做一些 hacky 内部 url 重写。这是 Pyramid 2.0 正在讨论的功能。
根据 Michael Merickel 的回答,目前这是不可能的。
但是有一个解决方法:
def main(global_config, **settings):
...
config.include(devices_include, route_prefix="/devices")
config.add_route('devices.collection', '/devices')
注意最后一行。
我正在尝试通过 including a callable 包含多个 config.add_route 函数来制作模块化金字塔应用程序。在我的 init.py:
def devices_include(config):
config.add_route("devices.collection", "/")
config.add_route("devices.single", "/{device_id}")
...
def main(global_config, **settings):
...
config.include(devices_include, route_prefix="/devices")
此代码有效,这意味着当我转到 /devices/
时,它 运行 是 devices.collection
函数,当我转到 /devices/1
时,它是 运行 函数devices.single
函数。但是,我希望在转到 /devices
时能够 运行 devices.collection
函数(没有尾部斜线)。我该怎么做?
随时深入探讨 https://github.com/Pylons/pyramid/issues/406 和许多相关问题。简短的回答是,如果你想让它工作,你需要停止使用 route_prefix
或者你可以做一些 hacky 内部 url 重写。这是 Pyramid 2.0 正在讨论的功能。
根据 Michael Merickel 的回答,目前这是不可能的。
但是有一个解决方法:
def main(global_config, **settings):
...
config.include(devices_include, route_prefix="/devices")
config.add_route('devices.collection', '/devices')
注意最后一行。