使用 Tipfy 进行全面路由

发布于 2024-09-27 21:27:30 字数 518 浏览 0 评论 0原文

使用tipfy,如果更具体的路由不匹配,如何在 urls.py 中表达包罗万象的路由?

Tipfy 使用类似 Werkzeug 的路由,因此(在 urls.py 中):

def get_rules(app): 
rules = [ 
    Rule('/<any>', endpoint='any', handler='apps.main.handlers.MainHandler'), 
    Rule('/', endpoint='main', handler='apps.main.handlers.MainHandler'), 
] 

这将匹配应用程序中的大多数随机入口点(app.example.com/fooapp.example.com/%20 等),但不包括 app.example.com/foo/bar 导致 404 的情况。

或者,是否有我缺少在 Tipfy 中处理 404 的优雅方法吗?

Using tipfy, how does one express a catch-all route in urls.py if more specific routes do not match?

Tipfy uses Werkzeug-like routing, so there's this (in urls.py):

def get_rules(app): 
rules = [ 
    Rule('/<any>', endpoint='any', handler='apps.main.handlers.MainHandler'), 
    Rule('/', endpoint='main', handler='apps.main.handlers.MainHandler'), 
] 

This will match most random entry points into the application (app.example.com/foo, app.example.com/%20 etc) but does not cover the app.example.com/foo/bar case which results in a 404.

Alternatively, is there a graceful way to handle 404 in Tipfy that I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

メ斷腸人バ 2024-10-04 21:27:30

我想你想要:

Rule('/', endpoint='any', handler='apps.main.handlers.MainHandler')

路径匹配器也匹配斜杠。

I think you want:

Rule('/<path:any>', endpoint='any', handler='apps.main.handlers.MainHandler')

The path matcher also matches slashes.

你是我的挚爱i 2024-10-04 21:27:30

也许您可以编写自定义中间件:

class CustomErrorPageMiddleware(object):    
def handle_exception(self, e):           
    return Response("custom error page")

要启用它,请在 tipfy 配置中添加某个位置:

   config['tipfy'] = {
       'middleware': [
           'apps.utils.CustomErrorPageMiddleware',
       ]
   }

它为您提供了相当大的灵活性 - 例如,您可以在某个地方发送邮件来通知存在问题。这将拦截应用程序中的所有异常

Maybe you could write custom middle ware:

class CustomErrorPageMiddleware(object):    
def handle_exception(self, e):           
    return Response("custom error page")

To enable it add somewhere to tipfy config:

   config['tipfy'] = {
       'middleware': [
           'apps.utils.CustomErrorPageMiddleware',
       ]
   }

It gives you quite a flexibility - you could for example send mail somewhere to inform that there was a problem. This will intercept all exceptions in your application

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文