網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.前期準(zhǔn)備
用戶models.py
class User(models.Model): ? ? username = models.CharField("用戶名",max_length=10)
點(diǎn)贊models.py
LikeNum的作用在于當(dāng)有人點(diǎn)贊時(shí)可以把它記錄下來(lái),包括點(diǎn)贊者和點(diǎn)贊的內(nèi)容
# 喜歡數(shù) 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'
發(fā)布models.py
Discusssion的作用在于渲染前端頁(yè)面,里邊包括動(dòng)態(tài)發(fā)布人和被點(diǎn)贊數(shù)量
# 我的討論 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
# 討論點(diǎn)贊 def addLikes(request,id): ?? ?# 識(shí)別出該登陸者用戶信息 ? ? if request.session.get('username') and request.session.get('uid'): ? ? ? ? username = request.session.get('username') ? ? ? ? user = UserInfos.objects.get(username=username) ? ? else: ? ? ?? ?# error 是自己寫(xiě)的出錯(cuò)頁(yè)面 ? ? ?? ?return HttpResponseRedirect('/error') ? ? ?? ? ? ? # 判別點(diǎn)贊的該Discussion是否存在,有可能在你點(diǎn)贊的時(shí)候該用戶已經(jīng)刪除,注意不能簡(jiǎn)單的使用if,else當(dāng)找不到discussion時(shí)會(huì)出錯(cuò) ? ? try: ? ? ? ? if Discussion.objects.get(id=id): ? ? ? ? ?? ?# 如果Discussion存在 ? ? ? ? ? ? d = Discussion.objects.get(id=id) ? ? ? ? ? ? # 如果User存在 ? ? ? ? ? ? if user: ? ? ? ? ? ? ?? ?# 判斷當(dāng)前用戶是否已經(jīng)給該Discussion點(diǎn)過(guò)贊 ? ? ? ? ? ? ?? ?# record 為該記錄,不存在時(shí)則自動(dòng)創(chuàng)建 ? ? ? ? ? ? ?? ?# flag 為當(dāng)前是否操作 ? ? ? ? ? ? ? ? record,flag = LikeNum.objects.get_or_create(user=user,discussion=d) ? ? ? ? ? ? ? ? # 如果剛剛創(chuàng)建 ? ? ? ? ? ? ? ? if flag: ? ? ? ? ? ? ? ? ? ? d.likes+=1 ? ? ? ? ? ? ? ? ? ? d.save() ? ? ? ? ? ? ? ? # 如果沒(méi)操作,說(shuō)明之前點(diǎn)過(guò)贊,此時(shí)再次點(diǎn)贊說(shuō)明是要取消點(diǎn)贊 ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? d.likes -= 1 ? ? ? ? ? ? ? ? ? ? d.save() ?? ??? ??? ??? ? ? ?# 并且刪除掉點(diǎn)贊記錄 ?? ??? ? ? ? ? ? ? ?LikeNum.objects.get(user=user,discussion=d).delete() ?? ??? ? ? ? ? ?# 跳轉(zhuǎn)到發(fā)布頁(yè)面 ? ? ? ? ? ? ? ? return render(request,'page.html',{'page':Discusssion.objects.all(),'ln':LikeNum.objects.fitter(user=user)}) ? ? ? ? ? ? else: ? ? ? ? ? ? ?? ?# 如果session中沒(méi)有用戶信息,則跳轉(zhuǎn)到登陸頁(yè)面 ? ? ? ? ? ? ? ? return redirect('/login') ? ? ? except Exception as e: ? ? ? ? # 否則跳轉(zhuǎn)到失敗頁(yè)面 ? ? ? ? return HttpResponseRedirect('/error')
2.html實(shí)現(xiàn)
{% for item in page %}?? ?用戶名:{{item.user.username}} ? ? ? ? ? ? ? ? ?{{item.likes}} ? ?{% endfor %}
3.js實(shí)現(xiàn)【?。?!注意這段代碼寫(xiě)在for循環(huán)之內(nèi)】
//ln指likenum【點(diǎn)贊數(shù)】,因?yàn)辄c(diǎn)贊記錄是QuerySet,需要從里邊遍歷 ?{% if ln %}? ?// 遍歷 ?{% for l in ln %}? ?// 當(dāng)當(dāng)前的discussion在LikeNum記錄里時(shí),為a標(biāo)簽添加一個(gè)class ?{% if l.discussion == item %} ?? ? {% endif %}? {%endfor%}? {%endif%}
4.css實(shí)現(xiàn)
.success { ? ? color: #fc5531; ? ? text-decoration: none; } a { ? ? text-decoration: none; ? ? color: #848B96; } a:hover { ? ? color: #fc5531; }
這只是一個(gè)大概流程,具體的美化還需要自己實(shí)現(xiàn),不懂得話可以留言來(lái)交流!
示意圖【我自己做出來(lái)的效果】
原文鏈接:https://blog.csdn.net/qq_44833392/article/details/123234145
相關(guān)推薦
- 2023-03-26 數(shù)據(jù)結(jié)構(gòu)TypeScript之鏈表實(shí)現(xiàn)詳解_其它
- 2022-09-18 jenkins配置golang?代碼工程自動(dòng)發(fā)布的實(shí)現(xiàn)方法_Golang
- 2023-04-18 MongoDB超大塊數(shù)據(jù)問(wèn)題解決_MongoDB
- 2023-02-25 Redisson如何解決redis分布式鎖過(guò)期時(shí)間到了業(yè)務(wù)沒(méi)執(zhí)行完問(wèn)題_Redis
- 2022-12-06 Python?list?append方法之給列表追加元素_python
- 2022-06-12 C語(yǔ)言數(shù)學(xué)問(wèn)題與簡(jiǎn)單DP01背包問(wèn)題詳解_C 語(yǔ)言
- 2022-08-14 PyTorch中torch.utils.data.DataLoader簡(jiǎn)單介紹與使用方法_pytho
- 2022-10-07 android?studio后臺(tái)服務(wù)使用詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支