【发布时间】:2023-04-09 18:58:01
【问题描述】:
使用 django 创建简单的通知页面。
我尝试了许多不同的代码,但没有一个概念适用于我的项目。每当我们点击通知页面时,都会显示在特定时间创建的帖子等通知。
这是我的意见.py
def notification(request):
goto = request.GET.get('goto', '')
notification_id = request.GET.get('notification', 0)
extra_id = request.GET.get('extra_id', 0)
if goto != '':
notification = Notification.objects.get(pk=notification_id)
notification.is_read = True
notification.save()
if notification.notification_type == Notification.MESSAGE:
return redirect('view_application',
application_id=notification.extra_id)
elif notification.notification_type == Notification.APPLICATION:
return redirect('view_application',
application_id=notification.extra_id)
return render(request, 'web/notification.html')
这是我的models.py
class Notification(models.Model):
MESSAGE = 'message'
APPLICATION = 'application'
CHOICES = (
(MESSAGE, 'Message'),
(APPLICATION, 'Application')
)
to_member = models.ForeignKey(Member, related_name='notification', on_delete=models.CASCADE)
notification_type = models.CharField(max_length=20, choices=CHOICES)
is_read = models.BooleanField(default=False)
extra_id = models.IntegerField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(Member, related_name='creatednotifications', on_delete=models.CASCADE)
class Meta:
ordering = ['created_at']
def str(自我): self:to_member
这是我的模板 notification.html
{% if not notification %}
No notifications yet!
{% endif %}
{% for notification in notification %}
<div class="notification">
<p>
{% if notification.notification_type == 'message' %}
<a href="{% url 'notification' %}?goto=view_application¬ification={{ notification.id }}&extra_id={{ notification.extra_id }}">
<strong>{{ notification.created_by.username }}</strong> sent you a message<br>
<small>{{ notification.created_at|timesince }} ago</small>
</a>
{% elif notification.notification_type == 'application' %}
<a href="{% url 'notification' %}?goto=view_application¬ification={{ notification.id }}&extra_id={{ notification.extra_id }}">
<strong>{{ notification.created_by.username }}</strong> applied for your job<br>
<small>{{ notification.created_at|timesince }} ago</small>
</a>
{% endif %}
所有通知都应显示在此处
【问题讨论】:
-
请列出您尝试过的方法。
标签: python html django notify concept