-1

Creating simple notification page using django.

I have tried many different code but none of the concepts worked for my project. whenever we click on the notification page notification like post created at a particular time should get displayed.

here is my views.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')

here is my 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): self:to_member

here is my template 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 %}

all the notification should appear here

James Z
  • 12,209
  • 10
  • 24
  • 44
SHAMILI
  • 17
  • 5

3 Answers3

0

You could try and use "django messages" if it's just normal notifications.

I hope this somehow answers your question.

ProfKache
  • 73
  • 5
0

models,py: Here is ref alos.

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)

generate an notification:

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')
Lord-shiv
  • 1,103
  • 2
  • 9
  • 19
0

From the code you have listed there it seems like you are not passing any data to your template, can you try add these before your render line:

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

then change you render line to this?

return render(request, 'web/notification.html', context)
Sirwill98
  • 324
  • 1
  • 13