显示Django消息框架消息

编程入门 行业动态 更新时间:2024-10-11 17:20:58
本文介绍了显示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消息框架消息

本文发布于:2023-11-25 11:07:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629500.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   框架   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!