Flask: get current route

77,301

Solution 1

the most 'flasky' way to check which route triggered your view is, by request.url_rule.

from flask import request

rule = request.url_rule

if 'antitop' in rule.rule:
    # request by '/antitop'

elif 'top' in rule.rule:
    # request by '/top'

Solution 2

Simply use request.path.

from flask import request

...

@app.route("/antitop/")
@app.route("/top/")
@requires_auth
def show_top():
    ... request.path ...

Solution 3

Another option is to use endpoint variable:

@app.route("/api/v1/generate_data", methods=['POST'], endpoint='v1')
@app.route("/api/v2/generate_data", methods=['POST'], endpoint='v2')
def generate_data():
    version = request.endpoint
    return version

Solution 4

If you want different behaviour to each route, the right thing to do is create two function handlers.

@app.route("/antitop/")
@requires_auth
def top():
    ...

@app.route("/top/")
@requires_auth
def anti_top():
    ...

In some cases, your structure makes sense. You can set values per route.

@app.route("/antitop/", defaults={'_route': 'antitop'})
@app.route("/top/", defaults={'_route': 'top'})
@requires_auth
def show_top(_route):
    # use _route here
    ...

Solution 5

It seems to me that if you have a situation where it matters, you shouldn't be using the same function in the first place. Split it out into two separate handlers, which each call a common fiction for the shared code.

Share:
77,301
Igor Chubin
Author by

Igor Chubin

Updated on July 05, 2022

Comments

  • Igor Chubin
    Igor Chubin almost 2 years

    In Flask, when I have several routes for the same function, how can I know which route is used at the moment?

    For example:

    @app.route("/antitop/")
    @app.route("/top/")
    @requires_auth
    def show_top():
        ....
    

    How can I know, that now route was called using /top/ or /antitop/?

    UPDATE

    I know about request.path I don't want use it, because the request can be rather complex, and I want repeat the routing logic in the function. I think that the solution with url_rule it the best one.

  • Igor Chubin
    Igor Chubin over 10 years
    I think that the solution with url_rule is the best one, but thank you anyway and +1
  • Michael Scheper
    Michael Scheper about 7 years
    Upvoting simply because you bothered with the import statement.
  • Ezequiel
    Ezequiel almost 5 years
    This is a good solution but consider @marcinkuzminski 's solution . I think it is more elegant.
  • gerardw
    gerardw almost 3 years
    This fails if the route is a variable rule @app.route('/user/<username>')
  • gerardw
    gerardw almost 3 years
    That shows the route with dots instead of slashes. That is, subunit.page instead of /subunit/page
  • OzzyTheGiant
    OzzyTheGiant over 2 years
    @MichaelScheper Agreed, so many code snippets for ANY type of code don't include import statements
  • winwin
    winwin over 2 years
    @gerardw request.url_rule.rule is the thing. It returns /user/<username> as really needed.