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.
You can use request.endpoint
:
{{ url_for(request.endpoint) }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With