Django Like count不会递增

时间:2017-04-24 11:41:38

标签: python django

我在我的网站上添加了一个类似按钮,点击它时会突出显示,但不增加喜欢的数量或显示管理中的计数。我不明白我做错了什么。请帮我解决这个问题。我的代码如下。

models.py

class Post(models.Model):
    post = models.TextField()
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    likes = models.IntegerField(default=0)

    def __str__(self):
        return self.post

views.py

@login_required
def about(request,pk):
    context = {}
    template = 'about.html'
    return render(request, template, context)
    post = get_object_or_404(Post, pk=pk)
    Post.objects.get(pk=pk)
    post_id = post.pk
    liked = False
    if request.session.get('has_liked_' + str(post_id), liked):
        liked = True
        print("liked {}_{}".format(liked, post_id))
    context = {'post': post, 'liked': liked}
    return render(request, 'imagec/about.html', {'post': post})



@login_required()
def like_post(request):
    liked = False
    if request.method == 'GET':
        post_id = request.GET['post_id']
        post = Post.objects.get(id=int(post_id))
        if request.session.get('has_liked_'+post_id, liked):
            print("unlike")
            if post.likes > 0:
                likes = post.likes - 1
                try:
                    del request.session['has_liked_'+post_id]
                except KeyError:
                    print("keyerror")
        else:
            print("like")
            request.session['has_liked_'+post_id] = True
            likes = post.likes + 1
    post.likes = likes
    post.save()
    return HttpResponse(likes, liked)

urls.py

url(r'like_post/$', imagec_views.like_post, name='like_post'),

about.html

<p>

    <strong id="like_count">{{ post.likes }}</strong> people like this category

{% if user.is_authenticated %}
    <input type="button" onclick="jQuery(this).toggleClass('active')" data-post_id="{{post.id}}" id="likes" value ='Like'>
{% endif %}

</p>

base.html文件

 <script type="text/javascript" src="{% static 'js/blog-ajax.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-3.js' %}"></script>
<script src="{% static 'js/jquery-3.js' %}"></script>
<script src="{% static 'js/blog-ajax.js' %}"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src = {% static "js/ajax-blog.js" %}> </script>

<script src = {% static "js/jquery-3.js" %}> </script>

Ajax的blog.js

$('#likes').click(function(){
    var postid;
    postid= $(this).attr("data-post_id");
    $.get('/imagec/like_post/', {post_id: postid}, function(data){
               $('#like_count').html(data);
    });
});

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用console.log侧的JSprint侧的Python进行调试。

<强> Ajax的blog.js

$('#likes').click(function(){
    var postid;
    postid= $(this).attr("data-post_id");
    console.log("Post id to be send " + postid);
    $.get('/imagec/like_post/', {post_id: postid}, function(data){
               console.log("data received: "+data);
               $('#like_count').html(data);
    });
});

<强> views.py

@login_required
def about(request,pk):
    context = {}
    template = 'about.html'
    return render(request, template, context)
    post = get_object_or_404(Post, pk=pk)
    Post.objects.get(pk=pk)
    post_id = post.pk
    liked = False
    if request.session.get('has_liked_' + str(post_id), liked):
        liked = True
        print("liked {}_{}".format(liked, post_id))
    context = {'post': post, 'liked': liked}
    return render(request, 'imagec/about.html', {'post': post})

@login_required()
def like_post(request):
    liked = False
    if request.method == 'GET':
        post_id = request.GET['post_id']
        post = Post.objects.get(id=int(post_id))
        if request.session.get('has_liked_'+post_id, liked):
            print("unlike")
            if post.likes > 0:
                likes = post.likes - 1
                try:
                    del request.session['has_liked_'+post_id]
                except KeyError:
                    print("keyerror")
        else:
            print("like")
            request.session['has_liked_'+post_id] = True
            likes = post.likes + 1
    post.likes = likes
    print("updated liked ", post.likes)
    post.save()
    return HttpResponse(likes, liked)

检查控制台打印。

除此之外,我最终有两个主要建议,

  • 不要使用get方法更新数据库中的值
  • 如果用户喜欢帖子,请不要使用会话来保存标记。那个逻辑可能会让你烦恼 在将来。