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?

4

1 回答 1

0

您需要将参数“next”传递给 url_for。

当您渲染模板时,将请求对象发送到模板:

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

然后在你的模板中你可以做

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

并且您将在登录时被重定向回当前页面...

于 2014-04-19T10:56:56.553 回答