如何显示`django-notification`的通知?

8 投票
2 回答
8329 浏览
提问于 2025-04-15 15:17

我一直在看关于 django-notification 的文档,里面讲了怎么创建通知,但没有说明怎么把这些通知展示给用户。有没有什么好的参考资料呢?我在网上搜索也没找到。如果没有的话,有人能给我一些建议吗?谢谢。

2 个回答

2

看看 Pinax,它的源代码可以在github上找到。他们在项目网站 http://code.pinaxproject.com 上经常使用通知功能。

编辑:
我刚刚看了一下。似乎Pinax要让这个功能正常工作,只需要在安装的应用列表中把它放在其他外部应用之前,并像往常一样包含它的URL文件。

4

答案是你需要把它整合到你自己的模板里。这可以简单到像下面这段代码:

<table>
    <caption>{% trans "Notices" %}</caption> 
    <thead>
        <tr>
            <th>{% trans "Type" %}</th>
            <th>{% trans "Message" %}</th>
            <th>{% trans "Date of the Notice" %}</th>
        </tr>
    </thead>
    <tbody>
        {% for notice in notices %}
            {% if notice.is_unseen %}
                <tr class="unseen_notice">
            {% else %}
                <tr class="notice">
            {% endif %}
                <td class="notice_type">[{% trans notice.notice_type.display %}]</td>
                <td class="notice_message">{{ notice.message|safe }}</td>
                <td class="notice_time">{{ notice.added|timesince }} {% trans "ago" %}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>

正如 @googletorp 所说Pinax 是了解作者如何使用 django-notification 的好地方。特别是,它有一个通知管理页面,可以作为一个很好的参考。

撰写回答