Flask-Bootstrap在一个块中结合了闪存消息 [英] Flask-Bootstrap combining flash message in one block

查看:207
本文介绍了Flask-Bootstrap在一个块中结合了闪存消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用了flask-bootstrap,剩下的唯一问题就是每个flash消息都显示在自己的block中,所以当页面显示3条消息时,它们堆积得相当快。总是以橙色,不管我给他们什么类别。有没有办法来覆盖这种行为,并在一个单一的块使用类别的正确的颜色结合闪光消息?

解决方案

这是我用来显示Flash消息的代码,我正在使用twitter bootstrap html / css / js 我手动添加的文件。要启用类别,您需要代码中的第二行。我有 {%if category =='message'%} ,因为默认的flash类别被称为'message'我想将其作为警告显示。您还必须确保您的类别名称与 css 匹配。基本类是'success','info','warning'和'danger' /rel =nofollow> bootstrap主题

 < div> 
{%with messages = get_flashed_messages(with_categories = true)%}
{%if messages%}
{%for category,message in message%}
{%if category = ='message'%}
< div class =alert alert-warningrole =alert>
{%else%}
< div class =alert alert - {{category}}role =alert>
{%endif%}
{{message}}
< / div>
{%endfor%}
{%endif%}
{%endwith%}
< / div>

为了让它们全部出现在一个块中,您只需要将模板中的for循环。

$ p $ {code $ {%with messages = get_flashed_messages(with_categories = true)%}
{%if messages% }
< div class =alert alert - {{messages.0.0}}role =alert>
{%用于类别,信息中的信息%}
{{信息}}< / br>
{%endfor%}
< / div>
{%endif%}
{%endwith%}

c $ c> message.0.0 是获得第一条消息的类别。您还可以查看 flash示例中的最后一个示例。 a>看看如何将不同的消息分类在一起。

I'm using flask-bootstrap in my project, and the only quirk left is that each flash message gets displayed in their own block, so they "pile-up" pretty fast when a page display 3 messages. And always in the orange color, no matter what category I gave them. Is there a way to override that behavior and combine flash message in a single block that use the right color of the category?

This is the code I use to display flash messages, I am using twitter bootstrap html/css/js files which I manually added. To enable the categories you need the second line in the code. I have the {% if category == 'message' %} because the default flash category is called 'message' and I want to display that as a warning. You also have to make sure that your category names match the css classes. The classes are 'success', 'info', 'warning' and 'danger' in the basic 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>

To get them all to appear within one block you simply have to move the for-loop in the template within the 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 %}

Where message.0.0 is to get the category of the first message. You can also look at the last example in the flash examples to see how to group together different message categories.

这篇关于Flask-Bootstrap在一个块中结合了闪存消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆