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

在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):
      ........
原文地址:https://www.cnblogs.com/JustRun1983/p/2603446.html