【发布时间】:2021-07-07 06:04:20
【问题描述】:
我正在构建一个通知系统。我几乎完成了通知系统。现在我的问题是通知内容没有呈现我的 html 模板。我不明白我在哪里做错了。这是我的代码:
通知 models.py:
class Notifications(models.Model):
blog = models.ForeignKey('blog.Blog',on_delete=models.CASCADE)
NOTIFICATION_TYPES = (('New Comment','New Comment'),('Comment Approved','Comment Approved'), ('Comment Rejected','Comment Rejected'))
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_from_user")
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="noti_to_user")
notification_type = models.CharField(choices=NOTIFICATION_TYPES,max_length=250)
text_preview = models.CharField(max_length=500, blank=True)
date = models.DateTimeField(auto_now_add=True)
is_seen = models.BooleanField(default=False)
views.py
def ShowNOtifications(request):
user = request.user
notifications = Notifications.objects.filter(user=user).order_by('-date')
Notifications.objects.filter(user=user, is_seen=False).update(is_seen=True)
template_name ='blog/notifications.html'
context = {
'notify': notifications,
}
return render(request,template_name,context)
#html
{% for notification in notifications %}
{% if notification.notification_type == "New Comment" %}
@{{ notification.sender.username }}You have received new commnet
<p>{{ notification.text_preview }}</p>
{%endif%}
{%endfor%}
为什么通知内容没有显示在我的 html 模板中?我在哪里做错了?
【问题讨论】:
-
这不是问题的原因
-
这行不通。我试过了
-
ahmad_fauzan458 你的解决方案是错误的。这是不同的场景
-
在你的上下文中你声明
'notify',所以在你的模板中你需要使用notify.sender.username而不是notification.sender.username -
我试过但没用
标签: django