如何使用ajax请求更新上下文处理器变量,而不刷新页面

如何使用ajax请求更新上下文处理器变量,而不刷新页面,ajax,django,django-context,Ajax,Django,Django Context,目前,我正在为新通知显示标题上的通知计数,它们有is_read=False,我想更新is_read=True并删除通知计数,下面是我正在做的 上下文处理器 def notification(request): """Provide the count of unread notifications for an authenticated user.""" if request.user.is_authenticated(): notificati

目前,我正在为新通知显示标题上的通知计数,它们有is_read=False,我想更新is_read=True并删除通知计数,下面是我正在做的

上下文处理器

  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>

我的模型已更新,但在我刷新页面之前,模板仍显示计数

您必须清除ajax成功时的通知计数:

success: function(response) {
    $("#notification_menu span").text("");
}

谢谢尼玛

由于上下文处理器在页面上运行,因此 我是这样做的

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