我正在尝试更改Pyramid hello world示例,以便对Pyramid服务器的任何请求都服务于同一页面.即所有路线指向同一视图.这是iv到目前为止所得到的:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/*')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
所有iv完成是改变行(来自hello world示例):
config.add_route('hello', '/hello/{name}')
Run Code Online (Sandbox Code Playgroud)
至:
config.add_route('hello', '/*')
Run Code Online (Sandbox Code Playgroud)
所以我希望这条路线成为"全能".我尝试了各种变化,无法让它工作.有没有人有任何想法?
谢谢你的补充
catchall路由的语法(在Pyramid中称为"遍历子路径")*subpath代替*.还有*traverse一种用于混合路由,它结合了路由调度和遍历.您可以在此处阅读:在路由模式中使用*子路径
在您的视图函数中,您将能够访问子路径request.subpath,这是由捕获路径捕获的路径段的元组.所以,你的应用程序看起来像这样:
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
if request.subpath:
name = request.subpath[0]
else:
name = 'Incognito'
return Response('Hello %s!' % name)
if __name__ == '__main__':
with Configurator() as config:
config.add_route('hello', '/*subpath')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8081, app)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
不要通过自定义404处理程序,它闻起来的PHP :)