Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current function name in the Flask template context

Tags:

python

flask

What I basically need is something like the following:

{{ url_for(current_view) }}

in a Jinja2 template, such that if the response came from:

@app.route('/')
def index():
    return render('index.html')

then current_view equals to "index", i.e., the actual view name, such that url_for works.

To provide some context, this problem actually came up when using flask-babel. I want to have a "switch language button" that basically redirects to the same url, but with a different language code, as in:

{{ url_for(current_view, lang_code="es") }}

if you are in the English page, and lang_code="en" if you are on the Spanish page.

like image 430
Alejandro Piad Avatar asked May 08 '15 01:05

Alejandro Piad


1 Answers

You can use request.endpoint:

{{ url_for(request.endpoint) }}
like image 63
Reto Aebersold Avatar answered Oct 20 '22 23:10

Reto Aebersold