显示Django消息框架消息 [英] Displaying Django Messages Framework Messages

查看:81
本文介绍了显示Django消息框架消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Django Messaging Framework 显示讯息给模板中的用户。

I have been using the Django Messaging Framework to display messages to a user in the template.

我正在输出模板,如下所示:

I am outputting them to the template like this:

<ul>
    {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>

这将输出所有的消息,错误,警告,成功等
我只是想知道如果任何人有任何想法如何只显示错误消息,如:

This outputs all the messages, errors, warning, success etc. I was just wondering if anyone had any ideas how to display only the error messages something like:

<ul>
    {% for message in messages.errors %}
        <li>{{ message }}</li>
    {% endfor %}
</ul>

目前为止,我所得到的最好的是:

The best I have come up with so far is this:

{% if messages %}
    {% for message in messages %}
        {% if forloop.first %}
            {% if message.tags == 'error' %}
                <div class="error">
                    <ul>
            {% endif %}
        {% endif %}

        <li>{{ message }}</li>

        {% if forloop.last %}
                </ul>
            </div>
        {% endif %}
    {% endfor %}
{% endif %}

任何想法?
提前感谢。

Any ideas? Thanks in advance.

推荐答案

你可以放一个等号:

<ul>
    {% for message in messages.errors %}
        {% if 'error' in message.tags %}<li>{{ message }}</li>{% endif %}
    {% endfor %}
</ul>

消息级别到消息标签的映射可以配置为 MESSAGE_TAGS

The mapping of message level to message tag can be configured with MESSAGE_TAGS.

这篇关于显示Django消息框架消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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