您的位置:首页 > 其它

Pyramid中如何配置多种URL匹配同一个View

2012-07-22 09:26 423 查看
在pylons中,通过配置Route可以很容易地配置不同的URL指向同一个controller的Action.

map.connect('/:page/category{categoryid}/pageindex{pageindex}/{id}', controller='front',action='index')
map.connect('/:page/category{categoryid}/pageindex{pageindex}/{id}/', controller='front',action='index')


但是在pyramid中,如果我们这样做,

config.add_route('home','/')
config.add_route('home','home/')


则会出现如下错误

pyramid.exceptions.ConfigurationConflictError

正确的实现方法是:

你需要为不同的URL配置不同的Route name(在单个application中是不允许有重复的route name):

config.add_route('home','/')
config.add_route('home1','home/')


然后为这些route配置相同的view

config.add_view(yourview, route_name='home')
config.add_view(yourview, route_name='home1')


如果是用@view_config decorator:

@view_config(route_name='home')
@view_config(route_name='home1')
def your_method(request):
........
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐