CherryPy 的 URL 生成器

发布于 2024-09-26 04:54:52 字数 357 浏览 1 评论 0原文

在使用 werkzeug 作为 Web 框架(它很棒而且简单,但不支持某些功能)之后,我现在正在尝试 Cherrypy。

现在我在cherrypy中怀念的是werkzeug使用装饰函数的名称构建url(例如模板中的链接)的优雅方式,如下所示:

@expose('/archive/<int:year>/<int:month>')
def archive(request, year, month):
    pass

>>> url_for('archive',2010,04)
'/archive/2010/04'

我在cherrypy中没有找到类似的方式,我错过了吗?

After using werkzeug as a web framework (which is great and simple, but doesnt support some features), i'm now trying cherrypy.

Now what I miss in cherrypy is werkzeug's elegant way of building urls (e.g. for links in templates) using the name of a decorated function like this:

@expose('/archive/<int:year>/<int:month>')
def archive(request, year, month):
    pass

>>> url_for('archive',2010,04)
'/archive/2010/04'

I didn't find a similar way in cherrypy, did I miss it?

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

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

发布评论

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

评论(1

情魔剑神 2024-10-03 04:54:52

你没有错过。 CherryPy 没有在“暴露”装饰器中内置这种方法。但是,您可以使用内置的 路由调度程序您的应用程序,它具有类似的 URL 模板语法。如果您想尝试将其包装到像 werkzeug 这样的装饰器中,我们很乐意看到代码粘贴在 http 上://tools.cherrypy.org 维基。将该逻辑粘贴到 RoutesDispatcher 类本身的奖励点。

You didn't miss it. CherryPy doesn't have that sort of approach built into the 'expose' decorator. You can, however, use the builtin Routes dispatcher with your application, which has a similar URL template syntax. If you'd like to try to wrap that up into a decorator like werkzeug's, we'd love to see the code pasted on the http://tools.cherrypy.org wiki. Bonus points for sticking that logic onto the RoutesDispatcher class itself.

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