页面加载时关闭Bootstrap警报

时间:2018-05-17 17:31:58

标签: django twitter-bootstrap twitter-bootstrap-3 django-messages

我有一个视图,我发送一些消息,如下:

messages.warning(request, 'Nothing found")

在我的settings.py中,我编辑了以下消息标记:

MESSAGE_TAGS = {
    messages.DEBUG: 'alert-info',
    messages.INFO: 'alert-info',
    messages.SUCCESS: 'alert-success',
    messages.WARNING: 'alert-warning',
    messages.ERROR: 'alert-danger'
}

在我的模板上,我这样显示:

{% for message in messages %}
    <div class="alert {{ message.tags }} alert-dismissible fade in">
        <button type="button" class="close" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        {% if message.tags == 'info' %}
            <p>{{ message }}<br>
                You have <strong>{{users}}</strong> user{{users|pluralize}}<br>
         ,around <strong>{{non_users}}</strong> non user{{non_users|pluralize}}</p>
        {% else %}
            <p>{{ message }}</p>
        {% endif %}
    </div>
{% endfor %}

加载模板后,消息显示得非常快并消失。

如果我试试这个:<p>{{message}}</p>而是信息留在那里,所以我猜测我在做bootstrap时出错了。

这个(有点)也有效:<div class="{{message.tags}}">

有人会发光吗?

修改:

我刚刚注意到这种情况发生在我使用messages.error()时,其余的工作正常,似乎无法找到错误。

1 个答案:

答案 0 :(得分:0)

试试这个,该类为alert-{{tag}},它将呈现alert-info,没有空格

{% for message in messages %}
    <div class="alert alert-{{message.tags}} page-alert">
        <button type="button" class="close" data-dismiss='alert'>
             <span aria-hidden="true"> × </span>
             <span class="sr-only">Close</span>
        </button>
        <p>{{message}}</p> 
    </div>
{% endfor %}
相关问题