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 I 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.
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.
If you want different behaviour to each route, the right thing to do is create two function handlers.
In some cases, your structure makes sense. You can set values per route.
the most 'flasky' way to check which route triggered your view is, by
request.url_rule
.Another option is to use endpoint variable:
Simply use
request.path
.