【问题标题】:Protecting part of a page with Flask-Security使用 Flask-Security 保护页面的一部分
【发布时间】:2014-06-01 13:15:45
【问题描述】:

我有一个由 Flask 生成的网页,它应该根据用户的状态显示内容。

目前,jinja2 模板的相关部分如下所示:

{% 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 %}

当有人点击“登录”链接时就会出现问题——此时他们只是被重定向回索引页面,而不是包含此模板的页面。

有什么方法可以在{{url_for()}} 上添加一个附加标志来提供&lt;a href&gt;,以确保用户在登录后被重定向回此页面?

【问题讨论】:

  • 如果您自己解决了这个问题,您可以将其添加为答案,然后接受它。无需在问题本身中添加答案。您无法关闭问题,但您当然可以添加答案。
  • 好的,谢谢。我试过了,但它说我在发帖 8 小时后才能回答我的问题。
  • 正确。稍后回来发布答案。

标签: python redirect flask jinja2 flask-security


【解决方案1】:

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

render_template 时,将请求对象发送到模板:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2014-10-10
    • 2014-12-21
    • 2011-04-20
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2016-09-01
    相关资源
    最近更新 更多