0

I am building an notifcation system. I am almost complete the notifcation system. Now my problem is notifcation content not rendering my html template. I am not understanding where I am doing mistake.here is my code:

notifications 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%}

why notification content not showing in my html template? where I am doing mistake?

7
  • it's not the cause of problem Commented Jul 7, 2021 at 6:07
  • It will not work. I tried Commented Jul 7, 2021 at 6:12
  • ahmad_fauzan458 your solution was wrong. it's different scenario Commented Jul 7, 2021 at 6:24
  • 1
    In your context you declare 'notify', so in your template you need use notify.sender.username instead of notification.sender.username Commented Jul 7, 2021 at 6:40
  • I tried but didn't work Commented Jul 7, 2021 at 6:42

2 Answers 2

1

In your template you want loop through notifications but Django template can't find notifications because you declare in your context with label 'notify'

context = {
    'notify': notifications,
}

So in your views.py, you can change 'notify' to 'notifications':

context = {
    'notifications': notifications,
}

You can see document here

Sign up to request clarification or add additional context in comments.

Comments

1

First thing you are doing wrong is using the value in dictionary to render in the template. Rather use the key, so your code should be:

{% for notification in notify %} 
{% if notification.NOTIFICATION_TYPES == "New Comment" %}

@{{ notification.sender}}You have received new commnet 
      <p>{{ notification.text_preview }}</p>
{%endif%}
{%endfor%}

9 Comments

This is not the solution . I tried this before posting the question
Also the variable date in the class and order_by date in your views are not matching.
may be this is not solving the issue however this is one of the issue you must fix, else you will not be able to render the data in template.
Farhan_Ahmed, Look man, it does not matter weather he has tried or not, whatever in the code he has mentioned that's not correct, at least he will get something corrected. You never use values to render into the templates, you always use keys. so with my response at least he will get some of the things corrected, may be there are some other issues which needs debug.
+1, this answer is correct. I think OP have some problem with his if statement. That's why the content not showing and he think this answer is incorrect.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.