• 点赞


    <div class="art_info clearfix">
                    <div class="left up_down">
                        <div class="up" onclick="info_post(this)">
                            <div><i class="fa fa-thumbs-up" aria-hidden="true"></i></div>
                            <div id="digg-up">赞一下({{ article_info.up_count }})</div>
                        </div>
                    </div>
                    <div class="left up_down clearfix">
                        <div class="down" onclick="info_post(this)">
                            <div><i class="fa fa-thumbs-down" aria-hidden="true"></i></div>
                            <div id="digg-down">踩一下({{ article_info.down_count }})</div>
                        </div>
                    </div>
                </div>
                <div class="clearfix">
                    {% if updown_info.updown__up == False %}
                        <div id="msg">你已点踩</div>
                    {% elif updown_info.updown__up == True %}
                        <div id="msg">你已点赞</div>
                    {% else %}
                        <div id="msg"></div>
                    {% endif %}
                </div>
    HTML
    <script src="/static/jquery-3.2.1.js"></script>
        <script src="/static/jquery.cookie.js"></script>
        <script>
            //点赞事件
            function info_post(cls) {
                var token = $.cookie('csrftoken');
                var article_id = {{ article_info.nid}}
                    $.ajax({
                        url: '/operate/up_down.html',
                        type: 'POST',
                        headers: {'X-CSRFToken': token},
                        data: {'article_id': article_id, 'way': $(cls).prop("className")},
                        dataType: 'JSON',
                        success: function (arg) {
                            if (arg.status == 0) {
                                $('#msg').text(arg.msg);//已经点过赞
                                if (arg.msg = "取消点踩") {
                                    $('#digg-down').text("踩一下" + "(" + (arg.count) + ")");
                                }
                                else {
                                    //点踩
                                    $('#digg-down').text("踩一下" + "(" + (arg.count) + ")");
                                }
                            }
                            if (arg.status == 1) {
                                $('#msg').text(arg.msg);//你已点踩
                                if (arg.msg == '取消点赞') {
                                    $('#digg-up').text("赞一下" + "(" + (arg.count) + ")");
                                }
                                else {
                                    //点赞
                                    $('#digg-up').text("赞一下" + "(" + (arg.count) + ")");
                                }
                            }
                        }
                    })
    <script>
    JS
    def up_down(request):
        """
        网页最终页点赞
        :param request:
        :return:
        """
        click_username = request.session.get('username')
        if not click_username:
            return redirect('/login/')
        way = request.POST.get('way')
        article_id = request.POST.get('article_id')
        count_obj = models.Article.objects.filter(nid=article_id).first()
        up_count = count_obj.up_count
        down_count = count_obj.down_count
        userinfo = models.UserInfo.objects.filter(username=click_username).first()
        click_username_id = userinfo.nid
        ret = {'status': None, 'msg': None, 'count': None}
        count = None
        try:
            with transaction.atomic():
                obj = models.UpDown.objects.filter(article_id=article_id, user_id=click_username_id).first()
                if obj.up == 1:
                    if way == "up":
                        # 取消点赞
                        # 删除关系表
                        models.UpDown.objects.filter(article_id=article_id, user_id=click_username_id).delete()
                        # 统计点赞数
                        count = up_count - 1
                        count = 0 if count <= 0 else count
                        models.Article.objects.filter(nid=article_id).update(up_count=count)
                        ret['status'] = 1
                        ret['msg'] = '取消点赞'
                    else:
                        # 点踩
                        ret['status'] = 0
                        count = down_count
                        ret['msg'] = '你已点赞'
                if obj.up == 0:
                    if way == "up":
                        # 点赞
                        ret['status'] = 1
                        count = up_count
                        ret['msg'] = '你已点踩'
                    else:
                        # 取消点踩
                        models.UpDown.objects.filter(article_id=article_id, user_id=click_username_id).delete()
                        count = down_count - 1
                        count = 0 if count <= 0 else count
                        models.Article.objects.filter(nid=article_id).update(down_count=count)
                        ret['status'] = 0
                        ret['msg'] = '取消点踩'
                        # 没有关系,既没有点赞也没有点踩
                        if way == "up":
                            models.UpDown.objects.create(article_id=article_id, user_id=click_username_id, up=1)
                            count = up_count + 1
                            models.Article.objects.filter(nid=article_id).update(up_count=count)
                            ret['status'] = 1
                            ret['msg'] = '点赞成功'
                        else:
                            models.UpDown.objects.create(article_id=article_id, user_id=click_username_id, up=0)
                            count = down_count + 1
                            models.Article.objects.filter(nid=article_id).update(down_count=count)
                            ret['status'] = 0
                            ret['msg'] = '点踩成功'
    
        except:
            pass
        ret['count'] = count
    
        return HttpResponse(json.dumps(ret))
    视图函数
  • 相关阅读:
    线程通信---wait notify
    线程安全方式03---Lock锁
    线程安全方式02Runnable---同步方法
    线程安全方式2Runnable---同步代码块
    线程安全方式1Thread类---同步代码块
    线程安全方式1Thread类---同步方法
    线程中的Thread类中的方法
    多线程之创建线程的方式
    python生成器
    python装饰器作业
  • 原文地址:https://www.cnblogs.com/domestique/p/7207529.html
Copyright © 2020-2023  润新知