簡體   English   中英

通過模板標簽Django檢查用戶是否喜歡該帖子

[英]Checking whether user did like the post or not through template tag Django

我正在使用模板標簽檢查用戶先前是否喜歡該供稿中的問題,以表明他是否喜歡“ red heart” img。 我將用戶個人資料和問題pk作為函數的參數發送,我想知道該用戶個人資料是否是喜歡該問題的人之一。 如何以正確的方式實施它?

那是我的模板標簽代碼

from django import template
from blog.models import UserProfile
from blog.models import Question
register = template.Library()

def likecheck(theuser, question_pk):
    question_object = Question.objects.get(pk=question_pk)
    # just rude logic of what I seek
    # if theuser in question_object.who_liked:
    #   return true
    # else:
    #   return false

register.filter('likecheck', like check)

models.py中的相關類:

class Question(models.Model):
    who_liked = models.ManyToManyField('UserProfile',    
    related_name='who_liked_QUESTION', blank=True, null=True)

我認為您只是在偽代碼中question_object.who_liked末尾缺少.all()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM