Python Pyramid中指向单个页面的所有地址(捕获到单个视图的所有路由)
我正在尝试更改,以便对金字塔服务器的任何请求都提供相同的页面。i、 e.所有路线都指向相同的视图。到目前为止,这就是iv所得到的:Python Pyramid中指向单个页面的所有地址(捕获到单个视图的所有路由),python,pyramid,Python,Pyramid,我正在尝试更改,以便对金字塔服务器的任何请求都提供相同的页面。i、 e.所有路线都指向相同的视图。到目前为止,这就是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
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()
所有iv操作都是更改行(来自hello world示例):
致:
所以我希望这条路线是“通吃”。我尝试了各种各样的变化,但都没能成功。有人有什么想法吗
感谢addvance你可以创建一个自定义的错误处理程序(我不记得了,但它在金字塔文档中)并捕获HTTP 404错误,然后从那里重定向/呈现你的全部捕获路由 我想到的链接是: 我做过这样的事情:
from pyramid.view import (
view_config,
forbidden_view_config,
notfound_view_config
)
from pyramid.httpexceptions import (
HTTPFound,
HTTPNotFound,
HTTPForbidden,
HTTPBadRequest,
HTTPInternalServerError
)
import transaction
import traceback
import logging
log = logging.getLogger(__name__)
#region Custom HTTP Errors and Exceptions
@view_config(context=HTTPNotFound, renderer='HTTPNotFound.mako')
def notfound(request):
if not 'favicon' in str(request.url):
log.error('404 not found: {0}'.format(str(request.url)))
request.response.status_int = 404
return {}
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()
我认为您应该能够从其中重定向到视图。catchall路由(在金字塔中称为“遍历子路径”)的语法是
*子路径
,而不是*
。还有*traverse
,用于混合路由,它将路由调度和遍历结合在一起。您可以在此处阅读:
在view函数中,您可以通过请求访问子路径。subpath
,它是catchall路由捕获的路径段的元组。因此,您的应用程序如下所示:
from pyramid.view import (
view_config,
forbidden_view_config,
notfound_view_config
)
from pyramid.httpexceptions import (
HTTPFound,
HTTPNotFound,
HTTPForbidden,
HTTPBadRequest,
HTTPInternalServerError
)
import transaction
import traceback
import logging
log = logging.getLogger(__name__)
#region Custom HTTP Errors and Exceptions
@view_config(context=HTTPNotFound, renderer='HTTPNotFound.mako')
def notfound(request):
if not 'favicon' in str(request.url):
log.error('404 not found: {0}'.format(str(request.url)))
request.response.status_int = 404
return {}
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()
不要通过定制的404处理程序,它有PHP的味道:)谢谢,这正是我想要的!我看了几遍文档的这一部分,但我只是不太习惯这个术语,所以它没有点击。谢谢