简体   繁体   中英

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

I am using template tag to check whether user liked the question in the feed previously in order to show him liked "red heart" img or otherwise not. I send user profile and pk of question as arguments to the functions, I want to know whether that user profile is one of that who liked the question. How can I implement it in the right way?

That's my template tag code

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)

Relevant piece of class in 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM