列出用户的关注者时出错

Error when listing out a user's followers

我正在尝试向我的社交媒体平台添加通知功能。其中一部分涉及在我网站顶部的导航栏中添加通知徽标,该徽标将显示已登录用户拥有的看不见的通知数量。

当我 运行 我的服务器时,我收到一个 NameError:

NameError

这是我的一部分 navbar.html:

{% load custom_tags %}
(...)
{% if user.is_authenticated %}
        <div class="nav-item dropdown">
            <a class="nav-link dropdown-toggle text-dark" data-bs-toggle="dropdown" href="#" role="button" aria-expanded="false"><i class="fas fa-user"></i></a>
            <ul class="dropdown-menu dropdown-menu-end">
              <li><a class="dropdown-item" href="{% url 'profile' request.user.profile.pk %}">Profile</a></li>
              <li><a class="dropdown-item" href="{% url 'account_logout' %}">Sign Out</a></li>
            </ul>
          </div>
          <div class="nav-item">
            {% show_notifications %}
        {% endif %}
      </div>
    </div>
</nav>

这是我的 show_notifications.html:

<div class="dropdown">
    <span class="badge bg-primary notification-badge">{{ notifications.count }}</span>
    <div class="dropdown-content d-none" id="notification-container">
        {% for notification in notifications %}
            {% if notification.post %}
                {% if notification.notification_type == 1 %}
                <div class="dropdown-item-parent">
                    <a href="#">@{{ notification.from_user }} liked your post</a>
                    <span class="dropdown-item-close">&times;</span>
                </div>
                {% elif notification.notification_type == 2 %}
                <div class="dropdown-item-parent">
                    <a href="#">@{{ notification.from_user }} commented on your post</a>
                    <span class="dropdown-item-close">&times;</span>
                </div>
                {% endif %}
            {% elif notification.comment %}
                {% if notification.notification_type == 1 %}
                <div class="dropdown-item-parent">
                    <a href="#">@{{ notification.from_user }} liked on your comment</a>
                    <span class="dropdown-item-close">&times;</span>
                </div>
                {% elif notification.notification_type == 2 %}
                <div class="dropdown-item-parent">
                    <a href="#">@{{ notification.from_user }} replied to your comment</a>
                    <span class="dropdown-item-close">&times;</span>
                </div>
                {% endif %}
            {% else %}
            <div class="dropdown-item-parent">
                <a href="#">@{{ notification.from_user }} has started following you</a>
                <span class="dropdown-item-close">&times;</span>
            </div>
            {% endif %}
            {% endfor %}
        </div>
    </div>

这是我的 custom_tags.py:

from django import template
from academiciesocial.models import Notification

register = template.Library()

@register.inclusion_tag('social/show_notifications.html', takes_context=True)
def show_notifications(context):
    request_user = context['request'].user
    notifiations = Notification.objects.filter(to_user=request_user).exclude(user_has_seen=True).order_by('-date')
    return {'notifications': notifications}
    

最后的notifications被标记为“未定义(PylancereportUndefinedVariable)”。

我的通知 class 来自我的 models.py:

class Notification(models.Model):
    # 1 = Like, 2 = Comment, 3 = Follow
    notification_type =  models.IntegerField()
    to_user = models.ForeignKey(User, related_name='notification_to', on_delete=models.CASCADE, null=True)
    from_user = models.ForeignKey(User, related_name='notification_from', on_delete=models.CASCADE, null=True)
    post = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='+', blank=True, null=True)
    comment = models.ForeignKey('Comment', on_delete=models.CASCADE, related_name='+', blank=True, null=True)
    date = models.DateTimeField(default=timezone.now)
    user_has_seen = models.BooleanField(default=False)
    

我对此还很陌生,所以非常感谢对问题的准确逐步解答:)

您的代码中有错字。

应该是“notifications”你做到了“notifiations