How do I use url_for if my method has multiple route annotations?

前端 未结 3 1837
独厮守ぢ
独厮守ぢ 2020-12-12 16:32

So I have a method that is accessible by multiple routes:

@app.route(\"/canonical/path/\")
@app.route(\"/alternate/path/\")
def foo():
    return \"hi!\"
         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-12 16:54

    Additionally, for those using a catch all route constructed with variables: Flask will correctly create the url path if url_for is passed a dictionary containing the variables.

    For example...

    app.py:

    app.route('/')
    app.route('//')
    def catch_all(pattern1, pattern2=None):
        return render_template('template.html', p1=pattern1, p2=pattern2)
    
    app.route('/test')
    def test_routing:
        args = {'pattern1': 'Posts', 'pattern2': 'create'}
        return render_template('test.html', args=args)
    

    test.html:

    click here
    

    When you click on the 'click here' link, you will be directed to the 'Posts/create' route.

提交回复
热议问题