【发布时间】:2020-02-15 05:29:50
【问题描述】:
class Post(models.Model):
POST_TYPE =(
("video","video"),
("pic","pic"),
)
upload_by = models.ForeignKey(User,on_delete=models.CASCADE)
caption = models.TextField()
post_type = models.CharField(max_length=100,choices=POST_TYPE)
media = models.FileField(upload_to='posts/')
created_at = models.DateTimeField(auto_now_add=True)
class PostLikes(models.Model):
post = models.ForeignKey(Post,on_delete=models.CASCADE)
like_by = models.ForeignKey(User,on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
这是我的 models.py 文件,我想知道当前登录用户是否喜欢我的模板中的帖子。我在我的模板文件中这样做
{% if request.user in i.postlikes_set.all%}
但我知道这是错误的,任何人都可以解决这个问题吗? 我想在我的模板中这样做
<a class="post-like-btn" href="#" {% if request.user in i.postlikes_set.all%} style="color: #e16a70;" {% else %} style="color:#b2b2b2 !important;" {% endif %}>
【问题讨论】:
标签: django python-3.x django-models django-templates django-views