0

I have a web page generated by Flask that should show content depending on the status of a user.

At the moment, the relevant part of the jinja2 template looks like this:

{% if service.spotting_data.loco_id %}
    Spotter Info *:<br />
    {% if current_user.is_authenticated() %}
        Loco: {{ service.spotting_data.loco_id }} <br />
        Consist: {{ service.spotting_data.consist_info }}
    {% else %}
        We have information on the locomotive and consist for this service, however you need to <a href="{{ url_for('security.login') }}"> log in </a> to see it
    {% endif %}
{% endif %}

The problem arises when someone clicks on the "log in" link - at the moment they just get redirected back to the index page and not the page that includes this template.

Is there any way to feed the <a href> with an additional flag on the {{url_for()}} to ensure that after log-in the user is redirected back to this page?

1 Answers1

0

You need to pass the argument "next" to url_for.

when you render_template, send the request object to the template:

render_template("template.html",request=request)

then in your template you can do

{{url_for('security.login',next=request.path)}}

and you will get redirected back to the current page on login...