Flask URL Route: Route Several URLs to the same function

前端 未结 2 852
无人共我
无人共我 2020-12-14 14:23

I am working with Flask 0.9.

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

/item/
/item// 
/item/&l         


        
相关标签:
2条回答
  • 2020-12-14 14:43

    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):
    
    0 讨论(0)
  • 2020-12-14 15:00

    Yes - you use the following construct:

    @app.route('/item/<int:appitemid>/<path:path>')
    @app.route('/item/<int:appitemid>', defaults={'path': ''})
    

    See the snippet at http://flask.pocoo.org/snippets/57/

    0 讨论(0)
提交回复
热议问题