Flask-Bootstrap 将 flash 消息合并在一个块中

Kin*_*olf 0 flask

我在我的项目中使用flask-bootstrap,唯一的怪癖是每条闪现消息都显示在自己的块中,因此当页面显示3条消息时,它们会很快“堆积”。并且始终为橙色,无论我给它们什么类别。有没有办法覆盖该行为并将闪存消息组合在使用正确类别颜色的单个块中?

Maz*_*vél 6

这是我用来显示 Flash 消息的代码,我正在使用bootstrap html/css/js手动添加的 twitter 文件。要启用类别,您需要代码中的第二行。我之所以有,是{% if category == 'message' %}因为调用了默认的 Flash 类别'message',并且我想将其显示为警告。您还必须确保您的类别名称与css classes. 基本引导主题中有classes“成功”、“信息”、“警告”和“危险” 。

<div>
  {% with messages = get_flashed_messages(with_categories=true) %}
      {% if messages %}
        {% for category, message in messages %}
            {% if category == 'message' %}
              <div class="alert alert-warning" role="alert">
            {% else %}
              <div class="alert alert-{{ category }}" role="alert">
            {% endif %}
              {{ message }}
            </div>
        {% endfor %}
      {% endif %}
  {% endwith %}
</div>
Run Code Online (Sandbox Code Playgroud)

为了让它们全部出现在一个块中,您只需将模板中的 for 循环移动到 div 内即可。

{% with messages = get_flashed_messages(with_categories=true) %}
  {% if messages %}
    <div class="alert alert-{{ messages.0.0 }}" role="alert">
      {% for category, message in messages %}
        {{ message }} </br>
      {% endfor %}
    </div>
  {% endif %}
{% endwith %}
Run Code Online (Sandbox Code Playgroud)

在哪里message.0.0获取第一条消息的类别。您还可以查看Flash 示例中的最后一个示例,了解如何将不同的消息类别分组在一起。

  • 谢谢,我设法使用 Flask-bootstrap flashed_messages 宏来完成这项工作:) (2认同)