【问题标题】:Django 2.0 Notifications: How to overwrite the default list.html page for django-notificationsDjango 2.0 通知:如何覆盖 django-notifications 的默认 list.html 页面
【发布时间】:2018-08-22 03:16:24
【问题描述】:

所以,我最近导入了Django-notifications 并成功添加了一两个通知。现在我想查看列表页面。在我的网址中,我添加了通知端点path('notifications/', include("notifications.urls")),,当我转到网址时,我得到the output that matches the documentation

现在,我该如何更改通知网址。我试图创建一个通知应用程序python manage.py startapp notifications,但它说已经存在一个。我觉得我错过了一些简单的东西,但我不能指望它。

【问题讨论】:

    标签: django django-notification


    【解决方案1】:

    您无法创建自己的应用程序notifications,因为您已经安装了一个名为notifications 的应用程序。这是你下载/安装并添加到your_project/settings.pyINSTALLED_APPS下的应用

    要查看默认列表,您可以python manage.py runserver,然后导航到localhost:8000/notifications/' to see the defaultlist.html`。

    我建议从那里创建您自己的列表。查看文档here,您会发现所有 QuerySet 方法。您可以基于这些查询构建视图。例如your-app/views.py:

    ...
    
    # Get all unread notifications for current user.
    def unread_notifications(request):
        context = {
            'notifications': request.user.notifications.unread()
        }
    
        return render(request, 'your-app/unread_notifications.html', context)
    

    还有你的your-app/unread_notifications.html(假设是引导程序):

    <ul class="notifications">
        {% for notice in notifications %}
        <div class="alert alert-block alert-{{ notice.level }}">
            <a class="close pull-right" href="{% url 'notifications:mark_as_read' notice.slug %}">
                <i class="icon-close"></i>
            </a>
    
            <h4>
                <i class="icon-mail{% if notice.unread %}-alt{% endif %}"></i>
                {{ notice.actor }}
                {{ notice.verb }}
                {% if notice.target %}
                of {{ notice.target }}
                {% endif %}
            </h4>
    
            <p>{{ notice.timesince }} ago</p>
    
            <p>{{ notice.description|linebreaksbr }}</p>
    
            <div class="notice-actions">
                {% for action in notice.data.actions %}
                <a class="btn" href="{{ action.href }}">{{ action.title }}</a>
                {% endfor %}
            </div>
        </div>
        {% endfor %}
    </ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 2017-04-26
      • 2014-01-05
      • 2014-12-17
      • 2012-03-15
      • 2016-06-04
      相关资源
      最近更新 更多