Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Ajax更新注释计数_Python_Jquery_Django - Fatal编程技术网

Python 如何使用Ajax更新注释计数

Python 如何使用Ajax更新注释计数,python,jquery,django,Python,Jquery,Django,我在Django做一个项目,用户可以在那里对帖子发表评论。当用户对帖子的评论将计数增加到1时,如何更新每篇帖子的评论计数。我试着把身份证添加到div上,但什么也没发生。我如何实现这一点 主模板: <!-- Comment count post is an object of all post in homepage --> <div class="col-4 col-md-4 col-lg-4" id="newfeeds-form"> <a href="{% url

我在Django做一个项目,用户可以在那里对帖子发表评论。当用户对帖子的评论将计数增加到1时,如何更新每篇帖子的评论计数。我试着把身份证添加到div上,但什么也没发生。我如何实现这一点

主模板:

<!-- Comment count post is an object of all post in homepage -->
<div class="col-4 col-md-4 col-lg-4" id="newfeeds-form">
<a href="{% url 'site:comments' post.id %}" class="home-comment-icon z-depth-0">
<img src="{{ '/static/' }}images/comment.png" width="19" height="19" alt="comment-icon">
{% if post.comments.all|length > 999 %}
<span class="font-weight-bold dark-grey-text" id="number-of-comments">
{{ post.comments.count|floatformat }}
</span>
{% else %}
<span class="font-weight-bold dark-grey-text" id="number-of-comments">
{{ post.comments.count }} Comment{{ post.comments.count|pluralize }}
</span>
{% endif %}
</a>
</div>

<!-- New Feeds Comment Form -->
<div id="newfeeds-form">
{% include 'ajax_newfeeds_comments.html' %} 
</div>

如果您确定每个请求都将创建一个新的注释,那么您可以通过增加所需html元素的计数来完成

到目前为止,我还没有使用python或django,但已经尝试过优化代码

{%如果post.comments.count>999%} {{post.comments.count | div:1000 | floatformat:1}}k评论 {%else%} {{post.comments.count}}Comment{{post.comments.count |复数化} {%endif%} 另一种选择是向请求提供有关评论数量的信息。 因此,您可以在jQuery中创建它,如下面的示例所示

$.ajax({
  // ...
  success: function(response) {]
    $('#newfeeds-form' + hiddenField.val()).html(response.form);
    textInput.val('');

    // your server side script should implement a new field "number_of_comments"
    refreshNumberOfComments(response.number_of_comments); // for this call the function above has to get a parameter
  },
  // ...
});

@UfguFugullu…您的代码一开始工作正常,但添加一个else语句注释计数不再增加。请看我的问题,我刚刚更新了。有没有可行的方法?1注意,2注意,如果超过999,则显示1我注意到您的代码中有错误。如果没有评论,当我提交第一条评论时,计数不会增加,除非我手动重新加载页面,然后我得到1,当我提交另一条评论时,计数会自动增加到2。我一直在尝试自己解决这个问题,它更复杂。计数只增加第一篇文章,当我在第二篇文章中提交评论时,计数不会增加,直到我手动重新加载我的页面为止。@Hize我已经编辑了我的答案,希望用它回答您的问题
function onSubmitFeedsForm(event) {
  // ...
  $.ajax({
    // ...
    success: function(response) {
      $('#newfeeds-form' + hiddenField.val()).html(response.form);
      textInput.val('');

      // how you can increment the value of the amount of comments
      refreshNumberOfComments();
    },
    // ...
  });
  // ...
}
// ...

function refreshNumberOfComments() {
  var numberOfCommentsElement = $('#number-of-comments');
  var numberOfComments = parseInt(numberOfCommentsElement.data('number')) + 1;

  numberOfCommentsElement.data('number', numberOfComments);

  if (numberOfComments == 1) {
    numberOfCommentsElement.text(numberOfComments + ' Comment');
  } else if (numberOfComments > 999) {
    numberOfCommentsElement.text((numberOfComments / 1000).toFixed(1) + 'k Comments');
  } else {
    numberOfCommentsElement.text(numberOfComments + ' Comments');
  }
}
$.ajax({
  // ...
  success: function(response) {]
    $('#newfeeds-form' + hiddenField.val()).html(response.form);
    textInput.val('');

    // your server side script should implement a new field "number_of_comments"
    refreshNumberOfComments(response.number_of_comments); // for this call the function above has to get a parameter
  },
  // ...
});