ajax like button正在用奇怪的值更新所有产品

时间:2017-10-11 01:35:36

标签: javascript python ajax django

我尝试在我的django项目中实现ajax like按钮。我想按钮在公司列表(列表视图)和详细视图中工作。但是在列表视图中,当我点击类似按钮时,类似的显示201应该只有2,而不像它显示200这样的东西而不是0.此外它更新所有的公司像价值而不是特定的公司谁的点击按钮。我的代码可能有什么问题?

这是代码

url(r'^like/company$',
    views.CompanyLikesAPIView.as_view(), name="like-company"),

class CompanyLikesAPIView(APIView):
    permission_classes = (permissions.IsAuthenticated, )

    def post(self, request, slug=None, format=None):
        company_id = request.data.get('id')
        print('company_id', company_id)
        action = request.data.get('action')
        if company_id and action:
            try:
                company = Company.objects.get(id=company_id)
                if action == "like":
                    company.likes.add(request.user)
                else:
                    company.likes.remove(request.user)
                return Response({'status': 'ok'})
            except:
                pass
        return Response({'status': 'ko'})


{% block pagecontent %}
    <div class="container">
        {% for company in companies %}
            {% with total_likes=company.likes.count users_like=company.likes.all %}
            {% if forloop.first %}<div class="row">{% endif %}
            <div class="col-md-4">
                    <div class="card-body text-center">
                        <h3 class="card-title text-center">{{ company.name }}</h3>
                        <a href="{{ company.get_absolute_url }}" class="btn btn-link">View Detail</a>
                        <span class="count">
                            <span class="total">{{ total_likes }}</span>
                            like{{ total_likes|pluralize }}
                        </span>
                        <a href="#" data-id="{{ company.id }}" data-action="{% if request.user in users_like %}un{% endif %}like" class="company-like like-{{ company.id}} btn btn-link"><i class="fa fa-thumbs-o-up"></i>
                        {% if request.user not in users_like %}
                            Like
                        {% else %}
                            Unlike
                        {% endif %}
                    </a>
            </div>
        </div>
        {% if forloop.counter|divisibleby:4 %}</div><br/><div class="row">{%endif %}
        {% if forloop.last %}</div>{% endif %}
    {% endwith %}
{% endfor %}
</div>
{% endblock pagecontent %}


$(document).ready(function(){
      $('a.company-like').click(function(e){
        e.preventDefault();
        $.post('{% url "products:like-company" %}',
            {
                id: $(this).data('id'),
                action: $(this).data('action')
            },
            function(data){
                if (data['status'] == 'ok')
                {
                    var previous_action = $('a.company-like').data('action');

                    // toggle data-action
                    $('a.company-like').data('action', previous_action == 'like' ? 'unlike' : 'like');
                    // toggle link text
                    $('a.company-like').text(previous_action == 'like' ? 'Unlike' : 'Like');

                    // update total likes
                    var previous_likes = parseInt($('span.count .total').text());
                    $('span.count .total').text(previous_action == 'like' ? previous_likes + 1 : previous_likes - 1);
                }
        });
    });
    }); 

我仅将django-rest-framework用于相似视图的原因是我将在稍后使用它来开发api。

0 个答案:

没有答案