查看“蓝鲸Django: 显示用户的评论与点赞”的源代码
←
蓝鲸Django: 显示用户的评论与点赞
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==实现点赞功能== ===点赞功能后台接口=== moments.models.py: <nowiki> class Reply(models.Model): status = models.ForeignKey(Status,models.CASCADE) author = models.CharField(max_length=50) type = models.CharField(max_length=20,choices=[("0","like"),("1","comment")]) at_person = models.CharField(max_length=50,null=True,blank=True) text = models.CharField(max_length=280,null=True,blank=True) def __str__(self): return "{} on {}".format(self.author,self.status.text) </nowiki> ===生成表=== [root@localhost wechat]# python3 manage.py makemigrations <nowiki>Migrations for 'moments': moments/migrations/0002_auto_20200614_0926.py - Create model Reply - Change Meta options on status - Alter field motto on wechatuser - Alter field pic on wechatuser - Add field status to reply</nowiki> [root@localhost wechat]# python3 manage.py migrate <nowiki>Operations to perform: Apply all migrations: admin, auth, contenttypes, moments, sessions Running migrations: Applying moments.0002_auto_20200614_0926... OK</nowiki> ===在admin中管理Reply表=== admin.py: <nowiki> from django.contrib import admin from .models import WeChatUser, Status, Reply # Register your models here. admin.site.register(WeChatUser) admin.site.register(Status) admin.site.register(Reply) </nowiki> ===admin后台中手动添加数据=== ====既有赞又有评论的数据==== 对第一条状态 添加两条赞 和两条评论 其中一个是作者发出的,一个是他人发出的。 [[文件:bd20-5-30.png]] [[文件:bd20-5-31.png]] [[文件:bd20-5-33.png]] [[文件:bd20-5-34.png]] [[文件:bd20-5-35.png]] ====光有赞的数据==== [[文件:bd20-5-36.png]] ====光有评论的数据==== [[文件:bd20-5-38.png]] ===后端调用数据show_status=== moments.show_status: <nowiki> from .models import Reply 。。。 @login_required def show_status(request): statuses = Status.objects.all() for status in statuses: likes = Reply.objects.filter(type="0",status=status) status.likes = likes comments = Reply.objects.filter(type="1",status=status) status.comments =comments return render(request,"status.html",{"statuses": statuses}) </nowiki> ===前端调用数据status.html=== 显示点赞的人: 在原点赞评论区域做如下修改: <nowiki>{% if status.likes or status.comments %} <div class="col-md-12" style="background-color:gray; padding-top:20px"> {% if status.likes %} <p style="font-size:30px"><span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> {% for like in status.likes %} {{ like.author }} {% if not forloop.last %}, {% endif %} {% endfor %} </p> {% endif %} <hr> <p style="font-size:30px">Monkey: Great!</p> <p style="font-size:30px">Po@Monkey: Thanks!</p> </div> {% endif %} </nowiki> 显示评论: 在上面的代码中继续添加: <nowiki> {% if status.likes or status.comments %} <div class="col-md-12" style="background-color:gray; padding-top:20px"> {% if status.likes %} <p style="font-size:30px"><span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> {% for like in status.likes %} {{ like.author }} {% if not forloop.last %}, {% endif %} {% endfor %} </p> {% endif %} {% if status.likes and status.comments %} <hr> {% endif %} {% for comment in status.comments %} <p style="font-size:30px"> {{ comment.author }} {% if comment.at_person %}@{{ comment.at_person }} {% endif %}: {{ comment.text }} </p> {% endfor %} </div> {% endif %} </nowiki>
返回至
蓝鲸Django: 显示用户的评论与点赞
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息