Django點贊的實現(xiàn)示例
1.前期準備
用戶models.py
class User(models.Model): ? ? username = models.CharField("用戶名",max_length=10)
點贊models.py
LikeNum的作用在于當有人點贊時可以把它記錄下來,包括點贊者和點贊的內容
# 喜歡數(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的作用在于渲染前端頁面,里邊包括動態(tài)發(fā)布人和被點贊數(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
# 討論點贊 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 為該記錄,不存在時則自動創(chuàng)建 ? ? ? ? ? ? ?? ?# flag 為當前是否操作 ? ? ? ? ? ? ? ? record,flag = LikeNum.objects.get_or_create(user=user,discussion=d) ? ? ? ? ? ? ? ? # 如果剛剛創(chuàng)建 ? ? ? ? ? ? ? ? if flag: ? ? ? ? ? ? ? ? ? ? d.likes+=1 ? ? ? ? ? ? ? ? ? ? d.save() ? ? ? ? ? ? ? ? # 如果沒操作,說明之前點過贊,此時再次點贊說明是要取消點贊 ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? d.likes -= 1 ? ? ? ? ? ? ? ? ? ? d.save() ?? ??? ??? ??? ? ? ?# 并且刪除掉點贊記錄 ?? ??? ? ? ? ? ? ? ?LikeNum.objects.get(user=user,discussion=d).delete() ?? ??? ? ? ? ? ?# 跳轉到發(fā)布頁面 ? ? ? ? ? ? ? ? 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實現(xiàn)
{% 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實現(xiàn) --!> {% endfor %}
3.js實現(xiàn)【?。?!注意這段代碼寫在for循環(huán)之內】
//ln指likenum【點贊數(shù)】,因為點贊記錄是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實現(xiàn)
.success { ? ? color: #fc5531; ? ? text-decoration: none; } a { ? ? text-decoration: none; ? ? color: #848B96; } a:hover { ? ? color: #fc5531; }
這只是一個大概流程,具體的美化還需要自己實現(xiàn),不懂得話可以留言來交流!
示意圖【我自己做出來的效果】
到此這篇關于Django點贊的實現(xiàn)示例的文章就介紹到這了,更多相關Django 點贊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Django錯誤:TypeError at / ''bool'' object is not callable解決
這篇文章主要介紹了Django 錯誤:TypeError at / 'bool' object is not callable解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08Python實戰(zhàn)之夢幻鋼琴小游戲的實現(xiàn)
這篇文章主要為大家詳細介紹了如何利用Python語言編寫一款界面化的(Tkinter)電子鋼琴小程序,文中的示例代碼講解詳細,感興趣的可以了解一下2023-02-02python3應用windows api對后臺程序窗口及桌面截圖并保存的方法
今天小編就為大家分享一篇python3應用windows api對后臺程序窗口及桌面截圖并保存的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08人工智能——K-Means聚類算法及Python實現(xiàn)
這篇文章主要介紹了人工智能——K-Means聚類算法及Python實現(xiàn),一個能夠找到我圈出的這?些點集的算法,就被稱為聚類算法,下面就來看看文章具體的介紹吧2022-01-01