zoukankan      html  css  js  c++  java
  • 博客系统-点赞取消

    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/"
                }
    
            });
  • 相关阅读:
    MySQL分库分表环境下全局ID生成方案
    机器码和字节码
    Java程序编译和运行的过程
    hive大数据倾斜总结
    图解MapReduceMapReduce整体流程图
    Java中的5种同步辅助类
    Tomcat 的三种(bio,nio.apr) 高级 Connector 运行模式
    RocketMQ与Kafka对比(18项差异)评价版
    ENode 2.0
    SecureCrt的操持连接办法
  • 原文地址:https://www.cnblogs.com/52-qq/p/8669464.html
Copyright © 2011-2022 走看看