博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
6.博客系统| 点赞功能
阅读量:4316 次
发布时间:2019-06-06

本文共 4569 字,大约阅读时间需要 15 分钟。

 

1.点赞样式的构建/点赞事件的绑定

点赞人即当前登录人;评论人即当前当前登录人。

article_detail.html

{% extends "base.html" %}{
% block content %}{
# {% load my_tags %}#}{
# {% multi_tag 3 9 %}#}
{
% endblock %}

 

base.html

    
Title
//先引入它

{

{ blog.title }} 管理

{
% block content %} {
# 写个块留着扩展用#} {% endblock %}

article_detail.css(点赞样式的)

.article_info .title{    margin-bottom: 20px;}#div_digg {  //父亲的    float: right;    margin-bottom: 10px;    margin-right: 30px;    font-size: 12px;    width: 125px;    text-align: center;    margin-top: 10px;}.diggit { //推荐的     float: left;    width: 46px;    height: 52px;    background: url("/static/font/upup.gif") no-repeat;  //把图片的静态文件下载到本地,博客它做反爬虫了    text-align: center;    cursor: pointer;    margin-top: 2px;    padding-top: 5px;}.buryit {  //反对的     float: right;    margin-left: 20px;    width: 46px;    height: 52px;    background: url("/static/font/downdown.gif") no-repeat;    text-align: center;    cursor: pointer;    margin-top: 2px;    padding-top: 5px;}.clear {    clear: both;  //清除浮动}

哪个用户对那篇文章进行了点赞,发Ajax请求,发生True or False

 

home_site.css

*{margin:0;padding:0;}.header{    width:100%;    height: 60px;    background-color: #369;}.header .title{    font-size: 18px;    font-weight: 100;    line-height: 60px;    color: white;    margin-left: 15px;    margin-top: -10px;;}.backend{    float:right;    color: white;    text-decoration: none;    font-size: 16px;    margin-right:10px;    margin-top: 10px;;}.pub_info{    margin-top: 10px;    color: darkgrey;}.menu{    margin-top: 20px;}

2. 文章点赞的保存

views.py

#点赞import jsondef digg(request):    print(request.POST)    article_id = request.POST.get("article_id")  #应该传布尔值,反序列化下,而不是字符串    is_up = json.loads(request.POST.get("is_up")) #它拿到的是字符串“True”,传的时候永远是一个真值,要反序列化传一个布尔值;    #点赞人即当前登录人    user_id = request.user.pk    ard = models.ArticleUpDown.objects.create(user_id=user_id, article_id=article_id,is_up=is_up)    return HttpResponse("ok")

点赞————>>>

3. 文章点赞数的数据同步

再次访问点赞这个页面时,应该保持这个点赞数。--->>> 在创建点赞记录的时候同时也要对文章进行更新。

#点赞import jsonfrom django.db.models import Fdef digg(request):    print(request.POST)    article_id = request.POST.get("article_id")    is_up = json.loads(request.POST.get("is_up")) #字符串的“True”,是一个布尔值    #点赞人即当前登录人    user_id = request.user.pk    ard = models.ArticleUpDown.objects.create(user_id=user_id, article_id=article_id,is_up=is_up)    queryset = models.Article.objects.filter(pk=article_id) #拿到这篇文章     if is_up:        queryset.update(up_count=F("up_count")+1) #对这篇文章点赞数进行更新     else:        queryset.update(down_count=F("down_count")+1)    return HttpResponse("ok")

 

4. 文章点赞的提示重复操作

#点赞import jsonfrom django.db.models import Ffrom django.http import JsonResponsedef digg(request):    print(request.POST)    article_id = request.POST.get("article_id")    is_up = json.loads(request.POST.get("is_up")) #字符串的“True”,是一个布尔值    #点赞人即当前登录人    user_id = request.user.pk    obj = models.ArticleUpDown.objects.filter(user_id=user_id, article_id=article_id).first() //过滤,用户对文章做了什么操作; 拿到这个对象。    response={
"state":True} #默认True if not obj: ard = models.ArticleUpDown.objects.create(user_id=user_id, article_id=article_id,is_up=is_up) queryset = models.Article.objects.filter(pk=article_id) if is_up: queryset.update(up_count=F("up_count")+1) else: queryset.update(down_count=F("down_count")+1) else: response["state"] = False #True表示你已经赞支持过了,False表已经踩过了 response["handled"] = obj.is_up #字典再加一个信息;过滤出的obj拿到is_up return JsonResponse(response)

 

 article_detail.html

5. 文章点赞数的Ajax更新

渲染有两种方式: render的渲染(刷新页面)、Ajax的dom显示(局部刷新),

代码的优化

View Code

 

转载于:https://www.cnblogs.com/shengyang17/p/9426561.html

你可能感兴趣的文章
线段树模板整理
查看>>
[教程][6月4日更新]VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!...
查看>>
[iOS问题归总]iPhone上传项目遇到的问题
查看>>
Python天天美味(总) --转
查看>>
Spring Framework tutorial
查看>>
【VS开发】win7下让程序默认以管理员身份运行
查看>>
【机器学习】Learning to Rank 简介
查看>>
Unity 使用实体类
查看>>
【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件...
查看>>
MySQL常见注意事项及优化
查看>>
流畅的Python (Fluent Python) —— 前言
查看>>
Jquery-menu-aim流畅的菜单滑动体验
查看>>
Jquery EasyUI修改行背景的两种方式
查看>>
生成器模式(Builder)C++实现
查看>>
Centos 7.5安装 Redis 5.0.0
查看>>
嵌入式Linux学习笔记(0)基础命令。——Arvin
查看>>
二分图匹配
查看>>
c++ 模板template
查看>>
javascript中的string对象
查看>>
CString的成员函数详解
查看>>