Flask URL路由:将多个URL路由到同一功能

编程入门 行业动态 更新时间:2024-10-27 04:29:41
本文介绍了Flask URL路由:将多个URL路由到同一功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Flask 0.9.

I am working with Flask 0.9.

现在我想将三个URL路由到相同的功能:

Now I want to route three urls to the same function:

/item/<int:appitemid> /item/<int:appitemid>/ /item/<int:appitemid>/<anything can be here>

<anything can be here>部分将永远不会在函数中使用.

The <anything can be here> part will never be used in the function.

我必须复制相同的函数两次才能实现此目标:

I have to copy the same function twice to achieve this goal:

@app.route('/item/<int:appitemid>/') def show_item(appitemid): @app.route('/item/<int:appitemid>/<path:anythingcanbehere>') def show_item(appitemid, anythingcanbehere):

会有更好的解决方案吗?

Will there be a better solution?

推荐答案

为什么不只使用默认值为None的可能为空的参数?

Why not just use a parameter that can potentially be empty, with a default value of None?

@app.route('/item/<int:appitemid>/') @app.route('/item/<int:appitemid>/<path:anythingcanbehere>') def show_item(appitemid, anythingcanbehere=None):

更多推荐

Flask URL路由:将多个URL路由到同一功能

本文发布于:2023-11-25 22:23:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   多个   功能   Flask   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!