博客系统-点赞取消_weixin_30732487的博客-程序员秘密

技术标签: json  

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/"
            }

        });

 

转载于:https://www.cnblogs.com/52-qq/p/8669464.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30732487/article/details/96812340

智能推荐

zynq 7000下读写qspi 及flash 唯一id_c 获取zynq7000 id_leon_zeng0的博客-程序员秘密

本文主要演示zynq 7000下对Qspi 的读写操作,以及读取8字节唯一ID, 可以用于简单加密。我在使用altera 的时候,写了一篇读写flash 及其ID的文章。在我转入zynq 7000的时候,自然也要有读写QSPI FLASH, 以及读取其ID。本文分为四部分:硬件设计,软件设计,测试验证,期待本文的前提条件是读者会helloworld 实验,也就是会zynq 7000的SDK开发过程...

acwing1053. 修复DNA(AC 自动机+dp)_做一只大熊猫的博客-程序员秘密

题意给定 n 个子串 ti 四个字符,和一个主串 s,所有字符串仅包含 A,G,C,T,问最少需要修改几个字符,才使 s 中不包含任意一个 ti 字符串。n &lt; 50, |s|&lt;=1000, |ti&lt;=20|.思路这是 y 总讲的状态机模型代码#include &lt;bits/stdc++.h&gt;using namespace std;#define db double#define ll long long#define Pir pair&lt;in

android webview崩溃处理,android webview shouldinterceptrequest覆盖即时崩溃_游子离的博客-程序员秘密

我在用android网络视图玩得很开心。我使用它来显示登录屏幕,然后在响应时截获验证代码。应该很简单…如果我只重写shouldroverrideurlloading,但如果我重写(就像android studio自动完成的那样),则我的webview加载和显示非常好:override fun shouldInterceptRequest(view: WebView?,request: WebRes...

【python代码】生成训练集和测试集路径, 写入train.txt和test.txt文件_train.txt python_程序猿未进化的博客-程序员秘密

import glob, os# 数据集的位置imgs_dir = '/img'print(imgs_dir)#用作 test 的图片数据的比例percentage_test = 10;#创建训练数据集和测试数据集:train.txt 和 test.txtfile_train = open('/train.txt', 'w')file_test = open('/test.txt', 'w')counter = 1index_test = round(100 / percentag

CentOS升级linux内核导致板载网卡不工作_centos升级内核网卡不可用_weixin_44444595的博客-程序员秘密

CentOS升级linux内核导致板载网卡不工作升级内核前环境CentOS7 原linux内核 3.10新linux内核 4.20板载网卡 Realtek 8111B故障现象系统成功升级到4.20内核后,板载网卡不工作,网线灯不亮。判断是新的网卡驱动模块 r8169有问题。解决过程下载 r8168网卡驱动替换4.20内核的 r8169模块。https://github.com/b...

QT 随笔 QProcess中waitForFinished的坑_WhiteTian的博客-程序员秘密

文章为个人创作,如转载请标注。当我们执行一个超过30s的动作的时候,int msecs = 30000,那么就会直接finish,像遇到解压文件这种情况并且要等finish时候用解压的文件的时候,就会出现问题。QString sCommandLine = "your command line";QProcess p;p.start(sCommandLine);p.waitForStarted(); p.waitForFinished(); //此处其实是有一个缺省参数的,...

随便推点

win7旗舰版dns服务器未响应,Win7系统DNS服务器未响应如何解决?_Denny W的博客-程序员秘密

最近有Win7系统用户反映,在连接网络的过程中,出现“DNS服务器未响应”的提示,导致网络连接失败,不能上网,用户并不知道这是怎么回事,也不知道该如何解决,为此非常苦恼。那么,Win7系统DNS服务器未响应要如何解决呢?接下来,小编就来教教大家Win7系统DNS服务器未响应的解决方法。方法/步骤1、右键点击任务栏上的网络图标,打开网络和共享中心;2、看一下这里的红叉在哪里?也就是查看完全映射。这张...

08-java代码块,继承,方法重写,final关键字_皓月千里0的博客-程序员秘密

代码块的分类和概述代码块概述:java中用{}括起来的代码被称为代码块代码块分类根据其位置和声明的不同,可分为局部代码块,构造代码块,静态代码块,同步代码块class Demo1_Code { public static void main(String[] args) { { int x = 10; //限定变量的声明

在微型计算机中硬件和软件的关系是_,关于微型计算机的硬件和软件的英语_科技体验者的博客-程序员秘密

有很多人觉得计算机英语很难学习,因为它涉及了很多的专业用语,所以小编今天就给大家整理了有关于计算机的英语,有时间要多多阅读计算机英语A computer is a fast and accurate symbol processing system. It can accept, store, process data and produce output results. A computer ...

Android自定义对话框_weixin_30856965的博客-程序员秘密

在android中有自带的对话框,为了美观,很多开发者会使用自定义对话框,如下图:点击“弹出自定义对话框按钮后”显示如图效果。首先要自己定义一个xml文件定义自己对话框的样式:&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;LinearLayout xmlns:android="http://schemas.android.c...

ORA-01157: cannot identify/lock data file n 故障一例_ora-01157: cannot identify/lock data file 14 - see_Leshami的博客-程序员秘密

最近在使用swingbench的时候碰到了ORA-01157故障,下面是其具体描述与解决。1、故障现象--查询视图dba_data_files时出现ORA-01157故障SQL> select file_name,tablespace_name from dba_data_files where tablespace_name='SOE';select file_name,tables