【问题标题】:creating notification using django使用 django 创建通知
【发布时间】: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&notification={{ 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&notification={{ 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


【解决方案1】:

如果只是普通通知,您可以尝试使用“django 消息”。

我希望这能以某种方式回答你的问题。

【讨论】:

  • 我们已经用过了。但它不起作用。
【解决方案2】:

从您在那里列出的代码看来,您似乎没有将任何数据传递给您的模板,您可以尝试在渲染行之前添加这些:

notifications = Notification.objects.get(is_read=False)
context = {"notifications":notifications}

然后把你的渲染线改成这个?

return render(request, 'web/notification.html', context)

【讨论】:

  • 在我的项目中进行了这些更改。还是不行。
【解决方案3】:

models,py:这里是refalos。

from django.db.models.signals import post_save
from notifications.signals import notify
from myapp.models import MyModel

def my_handler(sender, instance, created, **kwargs):
    notify.send(instance, verb='was saved')

post_save.connect(my_handler, sender=MyModel)

生成通知:

from notifications.signals import notify

notify.send(user, recipient=user, verb='you reached level 10')

// "recipient" can also be a Group, the notification will be sent to all the Users in the Group
notify.send(comment.user, recipient=group, verb=u'replied', action_object=comment,
            description=comment.comment, target=comment.content_object)

notify.send(follow_instance.user, recipient=follow_instance.follow_object, verb=u'has followed you',
            action_object=instance, description=u'', target=follow_instance.follow_object, level='success')

【讨论】:

  • 改进您的问题 添加更多详细信息。
  • 你现在能帮我吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 2020-08-25
  • 2015-08-31
  • 2013-12-14
  • 2018-05-31
相关资源
最近更新 更多