重定向后如何显示告警指示符?

问题描述

我有一个页面,可在成功单击提交按钮后将用户重定向到首页。

我要显示这样的指示器:

enter image description here

从提交按钮重定向回到首页导航栏下方的

我该怎么做?对不起,我是Web开发的新手。

解决方法

我相信您可以在flask中创建一个响应,该响应重定向并设置cookie。像

from flask import make_response

response = make_response(redirect('/homepage'))
response.set_cookie('show_success_flash','true')

return response

然后,在您的JS端,您可以读取cookie,如果为true,则显示消息。然后,您可以在关闭Flash消息时删除Cookie,也可以在setTimeout之后将其删除。像这样:

// homepage.js


const cookie = import 'js-cookie'

if (cookie.get('show_success_flash') {
  const successFlash = document.getElementById('success_flash_message')

  if (successFlash.style.display === 'none') {
    successFlash.style.display = 'block'
  }
}

,

Flask支持闪烁的消息。 https://flask.palletsprojects.com/en/1.1.x/patterns/flashing/

我认为,这比使用cookie显示临时弹出窗口更好。

要设置闪烁的消息,请执行以下操作:

@app.route('/sample')
def sample():
    if successCheck()
        flash('You were successfully logged in')
    return redirect("/index")

在模板中,要检索闪烁的消息,请执行以下操作:

{% with messages = get_flashed_messages() %}
  {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
      <li>{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}

您还可以使用类别:

# Route
flash(u'Invalid password provided','error')
# Template
{% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
    <ul class=flashes>
    {% for category,message in messages %}
      <li class="{{ category }}">{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}

或此模板选项:

# Template
{% with errors = get_flashed_messages(category_filter=["error"]) %}
{% if errors %}
<div class="alert-message block-message error">
  <a class="close" href="#">×</a>
  <ul>
    {%- for msg in errors %}
    <li>{{ msg }}</li>
    {% endfor -%}
  </ul>
</div>
{% endif %}
{% endwith %}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...