網站首頁 編程語言 正文
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 %}?? ?用戶名:{{item.user.username}} ? ? ? ? ? ? ? ? ?{{item.likes}} ? ?{% endfor %}
3.js實現【!!!注意這段代碼寫在for循環之內】
//ln指likenum【點贊數】,因為點贊記錄是QuerySet,需要從里邊遍歷 ?{% if ln %}? ?// 遍歷 ?{% for l in ln %}? ?// 當當前的discussion在LikeNum記錄里時,為a標簽添加一個class ?{% if l.discussion == item %} ?? ? {% 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
相關推薦
- 2022-11-09 ASP.NET?MVC視圖頁使用jQuery傳遞異步數據的幾種方式詳解_實用技巧
- 2022-07-21 Error: rsync: [sender] safe_read failed to read 4
- 2022-05-22 部署ASP.NET?Core程序到Linux系統_基礎應用
- 2022-11-22 python3.6.4安裝opencv3.4.2的實現_python
- 2022-05-03 如何利用Python實現簡易的音頻播放器_python
- 2022-06-12 基于Docker搭建iServer集群_docker
- 2024-07-13 解決mybatis中因數據庫列名和實體類屬性名不同而獲取不到數據的問題
- 2022-05-27 ASP.NET?Core中使用Swagger_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支