1

現在、新しい通知のヘッダーに通知カウントを表示しています。is_read = False があり、is_read = True を更新して通知カウントを削除したいです。これが私がやっていることです。

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 テンプレートと ajax 呼び出し

{% 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>

ビューの更新

クラス 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")

モデルが更新されましたが、ページを更新するまでテンプレートにカウントが表示されたままです

4

2 に答える 2

0

ありがとうニマ

コンテキストプロセッサがページレンダリングで実行されるため、次のようにしました

success: function(response) {
    $(this + ".badge .badge-important").html("");
}
于 2015-01-14T09:03:36.677 に答える
0

ajax 成功の通知カウントをクリアする必要があります。

success: function(response) {
    $("#notification_menu span").text("");
}
于 2015-01-14T08:54:16.573 に答える