1

Currently am displaying notifications count on header for new notifications, they have is_read = False, I want to update is_read = True and remove notifications count, Here is what i am doing

context_processor

  def notification(request):
      """Provide the count of unread notifications for an authenticated user."""
      if request.user.is_authenticated():
          notifications = Notification.objects.filter(user=request.user, is_read=False).\
                              order_by('-created')[:5]
      return {'notification_count': len(notifications),
            'notifications': notifications}
      else:
           return {}

html template and ajax call

{% if notification_count >= 1 %}
      <a id="notification_menu" data-toggle="dropdown" class="dropdown-toggle" href="#">
           <i class="ace-icon fa fa-bell icon-animated-bell"></i>
           <span class="badge badge-important">{{ payzaat_notification_count }}</span>
      </a>
 {% endif %}

<script type="text/javascript">
    (function($){
        $('#notification_menu').on("click",function(){
            console.log('clicked');
            $.ajax({
                    type: "GET",
                    url: "{% url 'parent:update_notification' %}",
                    dataType: 'json',
                    success: function(response){
                        return true;
                    },error:function(response){
                        return false;
                    }
                });
        });
    })(jQuery);
</script>

update view

class UpdateNotification(FormView):

@method_decorator(parent_required)
def dispatch(self, request, *args, **kwargs):
    return super(UpdateNotification, self).dispatch(request, *args, **kwargs)

def get(self, request, *args, **kwargs):
    for record in Notification.objects.filter(user=self.request.user, is_read=False):
        record.is_read = True
        record.save()
    return HttpResponse("OKAY")

My model got updated, but template still displaying count until i refresh my page

2 Answers2

0

You have to clear the notification count on ajax success:

success: function(response) {
    $("#notification_menu span").text("");
}
nima
  • 6,566
  • 4
  • 45
  • 57
0

Thanks nima

As context processors run on page render so I have done it like

success: function(response) {
    $(this + ".badge .badge-important").html("");
}