• 博客系统-点赞取消


    url配置

    # 路由分发配置url
    url(r'^up_count/',views.up_count), url(r'^down_count/',views.down_count),

    视图函数处理

    点赞

    def up_count(request):
        '''点赞函数,禁止同一用户点赞多次'''
        user_id = request.user.nid                   #当前用户的ID
        article_id = request.POST.get("article_id")  # 获取到当前的文章ID
        print("===================点赞")
        pollResponse = {"state": True}      #初始变量
        if models.ArticleUpDown.objects.filter(user_id=user_id,article_id=article_id):     #判断是否为同一用户点赞
            print("======去你大爷的,想点几次")
            pollResponse["state"]=False
        else:
            with transaction.atomic():
                models.ArticleUpDown.objects.create(user_id=user_id,article_id=article_id)      #创建一个新的用户
                models.Article.objects.filter(nid=article_id).update(up_count=F("up_count")+1)   #给文章的点赞数+1
    
        return HttpResponse(json.dumps(pollResponse))

    取消点赞

    def down_count(request):
        '''点赞函数,禁止同一用户点赞多次'''
        user_id = request.user.nid  # 当前用户的ID
        article_id = request.POST.get("article_id")  # 获取到当前的文章ID
    
        downResponse = {"state": True}
        if models.Article.objects.filter(nid=article_id,user_id=user_id).first():     #判断是否为同一用户点赞
            downResponse["state"]=False
        else:
            with transaction.atomic():
                print("=========可以到这里")
                models.ArticleUpDown.objects.create(nid=user_id,article_id=article_id)      #创建一个新的用户
                models.Article.objects.filter(nid=article_id).update(down_count=F("down_count")+1)   #给文章的点赞数+1
    
        print("===============>",downResponse)
    
        return HttpResponse(json.dumps(downResponse))

    前端点赞块

    <div class="updown row">
                {#        点赞块#}
                <div class="author_profile">
                    <div id="author_profile_info" class="author_profile_info">
                        <a href="" target="_blank"><img src="{{ current_user.avatar.url }}" width="50" height="50" alt="" class="author_avatar"></a>
                        <div id="author_profile_detail" class="author_profile_info">
                            {#                        头像快#}
                            <a href="">{{ current_user }}</a><br>
                            <a href="">关注--{{ current_user.fans.user }}</a><br>
                            <a href="">粉丝--{{ current_user.fans.follower }}</a><br>
                        </div>
                    </div>
                </div>
                <div class="buryit pull-right">
                        <span class="burynum" id="bury_count">{{ article_obj.down_count }}</span>
                </div>
    
                <div class="diggit pull-right">
                        <span class="diggnum" id="digg_count">{{ article_obj.up_count }}</span>
                </div>
            </div>

    js发送请求代码:

            $(".diggit").click(function () {
                if ($(".infos").attr("user_username")){
                    $.ajax({
                    url: "/blog/up_count/",
                    type: "POST",
                    data: {
                        csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
                        article_id:{{ article_obj.nid }}
                    },
                    success: function (data) {
                        data = JSON.parse(data);
                        if (data["state"]) {
                            var val = parseInt($("#digg_count").html()) + 1;
                            $("#digg_count").html(val)
                        } else {
                            $(".diggnum_error").html("请不要重复点赞").css("color", "red");
                            setTimeout(foo, 3000)
                        }
                    }
                })
                }else{
                    location.href="/login/"
                }
    
            });
            $(".buryit").click(function () {
                if ($(".infos").attr("user_username")){
                    $.ajax({
                    url: "/blog/down_count/",
                    type: "POST",
                    data: {
                        csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
                        article_id:{{ article_obj.nid }}
                    },
                    success: function (data) {
                        data = JSON.parse(data);
                        {#                    console.log(data);#}
                        if (data["state"]) {
                            var val = parseInt($("#bury_count").html()) + 1;
                            $("#bury_count").html(val)
                        } else {
                            $(".diggnum_error").html("去你大爷的,").css("color", "red")
                            setTimeout(foo, 3000)
                        }
                    }
                })
                }else{
                    location.href="/login/"
                }
    
            });
  • 相关阅读:
    AutoIT练习
    AutoIT键盘鼠标模拟组件
    javascript class 定义
    JIRA
    vs 命令窗口 常用命令
    jquery 获取参数 plugin
    THE VALUE OF TIME
    哈佛图书馆的二十条训言
    优秀javascript js组件集锦
    UVA 11205 The broken pedometer
  • 原文地址:https://www.cnblogs.com/52-qq/p/8669464.html
Copyright © 2020-2023  润新知