sudoz
sudoz

Reputation: 3553

How to redirect to a URL binding multi routes in Flask

Flask code:

@app.route('/debug')
@app.route('/debug/sample')
def debug():
    pass

Jinja2 code:

{{ url_for('app.debug') }}

How to control the real url that Jinja2 will redirect?

Upvotes: 1

Views: 303

Answers (1)

davidism
davidism

Reputation: 127190

Give each rule a different endpoint value. The default is the name of the decorated function, so both currently have the same name and the last registered takes precedence.

@app.route('/debug/sample', endpoint='debug_sample')
@app.route('/debug')
def debug():
    pass
url_for('debug')  # /debug
url_for('debug_sample')  # /debug/sample

Upvotes: 2

Related Questions