Django点赞的实现示例
作者:365JHWZGo 时间:2022-08-24 20:13:40
1.前期准备
用户models.py
class User(models.Model):
username = models.CharField("用户名",max_length=10)
点赞models.py
LikeNum的作用在于当有人点赞时可以把它记录下来,包括点赞者和点赞的内容
# 喜欢数
class LikeNum(models.Model):
user = models.ForeignKey(UserInfos,null=True,on_delete=models.SET_NULL)
discussion = models.ForeignKey(Discussion,null=True,on_delete=models.SET_NULL)
class Meta:
verbose_name_plural = 'user'
发布models.py
Discusssion的作用在于渲染前端页面,里边包括动态发布人和被点赞数量
# 我的讨论
class Discussion(models.Model):
user = models.ForeignKey(UserInfos,null=True,on_delete=models.SET_NULL)
likes = models.PositiveIntegerField("喜欢",default=0,editable=False)
class Meta:
verbose_name_plural = 'Discussion'
views.py
# 讨论点赞
def addLikes(request,id):
# 识别出该登陆者用户信息
if request.session.get('username') and request.session.get('uid'):
username = request.session.get('username')
user = UserInfos.objects.get(username=username)
else:
# error 是自己写的出错页面
return HttpResponseRedirect('/error')
# 判别点赞的该Discussion是否存在,有可能在你点赞的时候该用户已经删除,注意不能简单的使用if,else当找不到discussion时会出错
try:
if Discussion.objects.get(id=id):
# 如果Discussion存在
d = Discussion.objects.get(id=id)
# 如果User存在
if user:
# 判断当前用户是否已经给该Discussion点过赞
# record 为该记录,不存在时则自动创建
# flag 为当前是否操作
record,flag = LikeNum.objects.get_or_create(user=user,discussion=d)
# 如果刚刚创建
if flag:
d.likes+=1
d.save()
# 如果没操作,说明之前点过赞,此时再次点赞说明是要取消点赞
else:
d.likes -= 1
d.save()
# 并且删除掉点赞记录
LikeNum.objects.get(user=user,discussion=d).delete()
# 跳转到发布页面
return render(request,'page.html',{'page':Discusssion.objects.all(),'ln':LikeNum.objects.fitter(user=user)})
else:
# 如果session中没有用户信息,则跳转到登陆页面
return redirect('/login')
except Exception as e:
# 否则跳转到失败页面
return HttpResponseRedirect('/error')
2.html实现
{% for item in page %}
<div>
用户名:{{item.user.username}}
<a id="id{{item.id}}">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-like-fill" rel="external nofollow" ></use>
</svg>
<span id="nlikes">{{item.likes}}</span>
</a>
</div>
<!-- 请把我写在这里 3.js实现 --!>
{% endfor %}
3.js实现【!!!注意这段代码写在for循环之内】
//ln指likenum【点赞数】,因为点赞记录是QuerySet,需要从里边遍历
{% if ln %}
// 遍历
{% for l in ln %}
// 当当前的discussion在LikeNum记录里时,为a标签添加一个class
{% if l.discussion == item %}
<script>
obj = document.getElementById('id{{item.id}}');
obj.className = 'success';
</script>
{% endif %}
{%endfor%}
{%endif%}
4.css实现
.success {
color: #fc5531;
text-decoration: none;
}
a {
text-decoration: none;
color: #848B96;
}
a:hover {
color: #fc5531;
}
这只是一个大概流程,具体的美化还需要自己实现,不懂得话可以留言来交流!
示意图【我自己做出来的效果】
来源:https://blog.csdn.net/qq_44833392/article/details/123234145
标签:Django,点赞
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Python中break语句的用法
2021-12-21 22:18:17
![](https://img.aspxhome.com/file/2023/1/85381_0s.jpg)
css闭合浮动元素
2008-05-09 19:41:00
pycharm最新免费激活码至2099年(21.3.18亲测可用)
2023-10-02 09:53:20
![](https://img.aspxhome.com/file/2023/5/90685_0s.png)
六个Python编程最受用的内置函数使用详解
2022-06-12 22:26:01
获得MySQL改变字符集的方案
2010-08-31 14:53:00
直接生成XML的Google SiteMap的asp代码
2007-08-17 13:44:00
Python 相对路径报错:"No such file or directory"'原因及解决方法
2021-08-12 05:34:00
![](https://img.aspxhome.com/file/2023/5/66865_0s.png)
推荐8款常用的Python GUI图形界面开发框架
2023-04-09 04:02:41
![](https://img.aspxhome.com/file/2023/1/100751_0s.jpg)
从数据行入手保护SQL Server数据安全
2009-04-13 10:28:00
![](https://img.aspxhome.com/file/UploadPic/20094/2009413132414109.jpg)
Python实现随机漫步功能
2021-02-05 20:44:45
![](https://img.aspxhome.com/file/2023/3/117603_0s.png)
ASP调用系统ping命令代码
2008-04-27 20:45:00
PHP制作3D扇形统计图以及对图片进行缩放操作实例
2023-11-17 19:31:47
![](https://img.aspxhome.com/file/2023/9/91229_0s.png)
python原始套接字编程示例分享
2021-10-09 19:00:07
![](https://img.aspxhome.com/file/2023/8/72008_0s.jpg)
asp伪静态情况下实现的utf-8文件缓存实现代码
2011-02-24 10:49:00
django时区问题的解决
2022-06-09 10:56:45
详解利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击
2023-03-16 14:33:51
Python 12306抢火车票脚本 Python京东抢手机脚本
2023-02-13 14:22:21
Python骚操作完美实现短视频伪原创
2023-09-18 08:16:55
Python autoescape标签用法解析
2023-03-14 06:36:22
![](https://img.aspxhome.com/file/2023/1/87601_0s.png)
最基础的Python的socket编程入门教程
2022-10-13 03:38:46