【问题标题】:Can't change number of likes for post in tape with ajax, when click like button当单击“喜欢”按钮时,无法使用 ajax 更改磁带中帖子的点赞数
【发布时间】:2016-10-28 15:56:53
【问题描述】:

我正在尝试学习 django 等等。 计算磁带中每个帖子的点赞数时遇到问题。问题是我无法获得帖子的 ID。 如果我刷新页面,喜欢的数量会发生变化。但是用ajax改变它是真正的问题。 请解释一下,如果单击“喜欢”按钮,如何让磁带中每个帖子的点赞数变化。

Ajax 代码。

{% block jquery %}

function updatePostLikesCount(){
    var postlikescount = $(".post-likes-count")
    $.ajax({
        type: "GET",
        url: "/like_post/{{ post.id }}/post_likes_count/",
        success: function(data){
            postlikescount.html(data.count);
        },
        error: function(response, error){
        }
    })
}

$(".post-like").click(function(event){
    var img = $(this);
    event.preventDefault();
    $.ajax({
        url: img.parent().attr('href'),
        success: function(){
            updatePostLikesCount();
        },
        error: function(response, error){
        }
    })
});

{% endblock %}

这是帖子的磁带。

{% for post in tape %}
    ...
    {{ post.text }}
    ...
    <a href="/like_post/{{ post.id }}/">
        <img class="post-like"  src="{% static "" %}"/>
    </a>

            <span class="post-likes-count" >
                {{ post.like.liked_users.count }}
            </span>

{% endfor %}

这是一个统计帖子点赞数的视图

def post_likes_count(request, post_id, *args, **kwargs):
    if request.is_ajax():
        like = Like.objects.get(post_id=post_id)
        if like.liked_users.count == None:
            count = 0
        else:
            count = LikeTimestamp.objects.filter(like_id=like.id).count()
        return JsonResponse({
            'count': count,
        })
    else:
        raise Http404

否则,我尝试使用 like 重新加载页面元素,但失败了 :-)

【问题讨论】:

  • 你从哪里得到 {{post.id}} 值,请让页面重新加载,然后右键单击页面并检查 {{post.id}} 是否被实际替换id,这只是在我提出解决方案之前进行确认。
  • 嗯... $(".post-like").click(function(event){ 中的 id 没有问题 function updatePostLikesCount(){url: "/like_post/{{ post.id }}/post_likes_count/", 有问题,它试图发送此 url url: "/like_post//post_likes_count/",

标签: javascript jquery ajax django posts


【解决方案1】:

更新:

{% block jquery %}
    function updatePostLikesCount(postlikescount){
        $.ajax({
            type: "GET",
            url: "/like_post/"+postlikescount.data().id+"/post_likes_count/",
            success: function(data){
                alert('class is '+postlikescount.parent().find('.post-likes-count').attr('class'));
                postlikescount.parent().find('.post-likes-count').html(data.count).show();
                alert('likes count '+data.count);
            },
            error: function(response, error){
            }
        })
    }

    $(".post-like").click(function(event){
        var img = $(this);
        event.preventDefault();
        $.ajax({
            url: img.parent().attr('href'),
            success: function(){
                var like = img.parent();
                updatePostLikesCount(like);
            },
            error: function(response, error){
            }
        })
    });
{% endblock %}

像这样稍微改变一下视图(添加 data-id):

{% for post in tape %}
    ...
    {{ post.text }}
    ...
    <a href="/like_post/{{ post.id }}/" data-id="{{post.id}}">
        <img class="post-like"  src="{% static "" %}"/>
    </a>

     <span class="post-likes-count" {% if post.like.liked_users.count == 0 %}style="display:none;"{% endif %}>
                {{ post.like.liked_users.count }}
     </span>
{% endfor %}

【讨论】:

  • 只要在span标签中保留一个if elsehide,如果计数为0,我对python没有经验,所以我想你可以编辑if else条件如果它是错了。
  • 点赞数不变。但是链接/like_post/"+postlikescount.data().id+"/post_likes_count/ 发送带有帖子ID,酷!
  • 你能把这个alert(JSON.stringify(data)); 添加到更新喜欢计数的成功函数中并告诉我输出吗?
  • 它告诉警报中的喜欢数。如果一次又一次点击按钮,数字会发生变化
  • 然后将此.html(data.count) 更改为.html(data)&lt;span class="post-likes-count&gt;" 出现在点赞数为 0 的帖子中吗?
【解决方案2】:

一些改进方法。

首先...您始终希望计数的 html 存在,并且只有在没有喜欢或显示为零时才应隐藏它。否则,您需要在第一次投票时使用 javascript 添加 html

下一个

var postlikescount = $(".post-likes-count");

这将包括页面中具有该类的所有元素....不是您单击按钮时想要的特定元素。

相反,您可以将您想要的参数作为参数传递给updatePostLikesCount()

改变

function updatePostLikesCount(){
        var postlikescount = $(".post-likes-count");

function updatePostLikesCount(postlikescount){

并且在点击处理程序中修改所以我们传入适当的元素

success: function() {
     // which count element
     var $countElement = img.next();
     // pass to function
     updatePostLikesCount($countElement);
}

【讨论】:

  • 嗨@charlietfl!我添加了您的代码,每次单击“喜欢”按钮时它都会重新加载我的整个页面。
  • 可能会在您的浏览器控制台中引发错误。总是先看那里
  • 是的,有一个错误ReferenceError: postlikescount is not defined
  • 您是否按照我一开始的建议在页面中添加了&lt;span class="post-likes-count" &gt;
  • 是的,从一开始就存在。
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 2019-11-07
  • 2011-09-22
相关资源
最近更新 更多