Python 金字塔中的所有地址都转到单个页面(到单个视图的全部路由)
All addresses to go to a single page (catch-all route to a single view) in Python Pyramid
我正在尝试更改 Pyramid hello world example 以便对金字塔服务器的任何请求都提供相同的页面。即所有路线都指向同一视图。这是 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()
iv 所做的只是更改行(来自 hello world 示例):
config.add_route('hello', '/hello/{name}')
收件人:
config.add_route('hello', '/*')
所以我希望路线是 'catch-all'。我尝试了各种变体,但无法正常工作。有人有什么想法吗?
提前致谢
您可以创建一个自定义错误处理程序(我不记得了,但它在 Pyramid 文档中)并捕获 HTTP 404 错误,然后 redirect/render 从那里开始您的包罗万象的路线.
我想到的link:http://docs.pylonsproject.org/projects/pyramid//en/latest/narr/hooks.html
我做过这样的事情:
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 {}
我认为你应该能够从那里重定向到一个视图。
catchall 路由(在 Pyramid 中称为 "traversal subpath")的语法是 *subpath
而不是 *
。还有 *traverse
用于结合路由调度和遍历的混合路由。您可以在这里阅读:Using *subpath in a Route Pattern
在您的视图函数中,您将能够通过 request.subpath
访问子路径,这是一个由 catchall 路由捕获的路径段的元组。因此,您的应用程序将如下所示:
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 :)
我正在尝试更改 Pyramid hello world example 以便对金字塔服务器的任何请求都提供相同的页面。即所有路线都指向同一视图。这是 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()
iv 所做的只是更改行(来自 hello world 示例):
config.add_route('hello', '/hello/{name}')
收件人:
config.add_route('hello', '/*')
所以我希望路线是 'catch-all'。我尝试了各种变体,但无法正常工作。有人有什么想法吗?
提前致谢
您可以创建一个自定义错误处理程序(我不记得了,但它在 Pyramid 文档中)并捕获 HTTP 404 错误,然后 redirect/render 从那里开始您的包罗万象的路线.
我想到的link:http://docs.pylonsproject.org/projects/pyramid//en/latest/narr/hooks.html
我做过这样的事情:
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 {}
我认为你应该能够从那里重定向到一个视图。
catchall 路由(在 Pyramid 中称为 "traversal subpath")的语法是 *subpath
而不是 *
。还有 *traverse
用于结合路由调度和遍历的混合路由。您可以在这里阅读:Using *subpath in a Route Pattern
在您的视图函数中,您将能够通过 request.subpath
访问子路径,这是一个由 catchall 路由捕获的路径段的元组。因此,您的应用程序将如下所示:
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 :)