Python 在模板中检查用户是否对帖子进行了投票

Python 在模板中检查用户是否对帖子进行了投票,python,django,django-models,Python,Django,Django Models,这是我的Post模型: 型号 class Post(models.Model): user = models.ForeignKey(User, blank=True, null=True) title = models.TextField(max_length=76) content = models.TextField(null=True, blank=True) ... class PostScore(models.Model): user = m

这是我的
Post
模型:

型号

class Post(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    title = models.TextField(max_length=76)
    content = models.TextField(null=True, blank=True)
    ...


class PostScore(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    post = models.ForeignKey(Post, related_name='score')
    upvotes = models.IntegerField(default=0)
    downvotes = models.IntegerField(default=0)
这是我的模板。我想做这样的事。。。如果用户对帖子投了向上投票或向下投票,则隐藏向上投票/向下投票按钮:

{% if request.user in Post.has_answered %}
{% else %}
    <img src="upvote.png" class="upvote" />
    <img src="downvote.png" class="downvote" />
{% endif %}

你知道我该怎么解决这个问题吗?我不太确定错误消息,因为我认为我无法更改当前的
用户
字段。

您是否尝试过将
标志
从django视图发送到模板,如:

def myView(request):

    parameters['is_user_voted'] = PostScore.objects.filter(user=self.request.user).exists()
    .....
    .....
    send ```parameters``` to your template using render()
并将模板更改为:

{% if is_user_voted == 'True' %}
{% else %}
    <img src="upvote.png" class="upvote" />
    <img src="downvote.png" class="downvote" />
{% endif %}
{%if is_user_voted=='True%}
{%else%}
{%endif%}

您是否尝试过将django视图中的
标志发送到模板,如:

def myView(request):

    parameters['is_user_voted'] = PostScore.objects.filter(user=self.request.user).exists()
    .....
    .....
    send ```parameters``` to your template using render()
并将模板更改为:

{% if is_user_voted == 'True' %}
{% else %}
    <img src="upvote.png" class="upvote" />
    <img src="downvote.png" class="downvote" />
{% endif %}
{%if is_user_voted=='True%}
{%else%}
{%endif%}

您可以将型号更改为这样。您可能不需要PostScore模型

class Post(models.Model):
    # Other Fields i.e title, content, author ...
    upvotes = models.ManyToMany(User)
    downvotes = models.ManyToMany(User)
你可以用这个在帖子上获得投票

upvotes = post_object.upvotes.count()
downvotes = post_object.downvotes.count()
要查看用户是否已向上投票

if request.user in post_object.upvotes.all():
    # This user has upvoted this post
反对票也一样

您也可以在模板中执行类似的操作,并根据条件隐藏/显示按钮

{% if request.user in post_object.upvotes.all %}
    <!-- show upvote button highlighted -->
{% elif request.user in post_object.downvotes.all %}
    <!-- show downvote button highlighted -->
{% else %}
    <!-- Show both buttons (not highlighted) -->
{% endif %}
{%if post_object.upvoces.all%}
{%elif request.user在post_object.downvoces.all%}
{%else%}
{%endif%}

希望这能有所帮助。

您可以将您的型号更改为这样。您可能不需要PostScore模型

class Post(models.Model):
    # Other Fields i.e title, content, author ...
    upvotes = models.ManyToMany(User)
    downvotes = models.ManyToMany(User)
你可以用这个在帖子上获得投票

upvotes = post_object.upvotes.count()
downvotes = post_object.downvotes.count()
要查看用户是否已向上投票

if request.user in post_object.upvotes.all():
    # This user has upvoted this post
反对票也一样

您也可以在模板中执行类似的操作,并根据条件隐藏/显示按钮

{% if request.user in post_object.upvotes.all %}
    <!-- show upvote button highlighted -->
{% elif request.user in post_object.downvotes.all %}
    <!-- show downvote button highlighted -->
{% else %}
    <!-- Show both buttons (not highlighted) -->
{% endif %}
{%if post_object.upvoces.all%}
{%elif request.user在post_object.downvoces.all%}
{%else%}
{%endif%}

希望这能有所帮助。

这确实是一个正确的解决方案,但如果说我们有,这不是一个性能问题吗≈1000名用户和/或≈1000张赞成票/反对票?我不能在我的
Post
模型中添加另一个
User
字段,否则我会得到原始问题中显示的错误。我已经有
User=models.ForeignKey(User,blank=True,null=True)
,它是
Post
的作者,我不能删除它。@EzzatA可能使用exists()可以提高性能。这确实是一个正确的解决方案,但如果我们有,这不是一个性能问题吗≈1000名用户和/或≈1000张赞成票/反对票?我不能在我的
Post
模型中添加另一个
User
字段,否则我会得到原始问题中显示的错误。我已经有
User=models.ForeignKey(User,blank=True,null=True)
,它是
Post
的作者,我不能删除它。@EzzatA可能使用exists()可以提高性能。