【发布时间】:2015-07-18 07:46:29
【问题描述】:
我在我的项目中使用了flask-bootstrap,唯一的怪癖是每条Flash消息都显示在它们自己的块中,所以当一个页面显示3条消息时,它们会很快“堆积”起来。而且总是橙色,不管我给他们什么类别。有没有办法覆盖该行为并将 Flash 消息组合在使用正确类别颜色的单个块中?
【问题讨论】:
-
发布您用来显示闪现消息的代码
标签: flask
我在我的项目中使用了flask-bootstrap,唯一的怪癖是每条Flash消息都显示在它们自己的块中,所以当一个页面显示3条消息时,它们会很快“堆积”起来。而且总是橙色,不管我给他们什么类别。有没有办法覆盖该行为并将 Flash 消息组合在使用正确类别颜色的单个块中?
【问题讨论】:
标签: flask
这是我用来显示 Flash 消息的代码,我正在使用手动添加的 twitter bootstrap html/css/js 文件。要启用类别,您需要代码中的第二行。我有{% if category == 'message' %},因为默认的闪存类别称为'message',我想将其显示为警告。您还必须确保您的类别名称与css classes 匹配。 classes 是基本bootstrap theme 中的“成功”、“信息”、“警告”和“危险”。
<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>
要让它们全部出现在一个块中,您只需在 div 中移动模板中的 for 循环。
{% 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 %}
其中message.0.0 是获取第一条消息的类别。您还可以查看flash examples 中的最后一个示例,了解如何将不同的消息类别组合在一起。
【讨论】: