不同的文本取决于用户是否喜欢pos

2024-03-28 19:14:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我的模板中有这个if语句,根据用户是否喜欢帖子来显示不同的按钮。有相同的触发链接、url和视图,但是文本要么说“喜欢”要么说“不喜欢”,这取决于用户以前是否喜欢那篇文章。但是它不工作,没有错误,只是不工作。你知道吗

likes是ManyToManyField模型中的UserPost。你知道吗

模板:

{% for post in posts %}
    <div class="single-post">
        <div class="post-header">
            <a class="link inline-block" href="{% url 'feed:post_detail' post.id %}">
                <h2 class="post-title">{{ post.title }}</h2>
            </a>

            <div class="post-interaction">
                {% if request.user.username in post.likes %}
                    <a class="link right-margin" href="{% url 'feed:post_like' post.id %}">
                        Unlike: {{ post.likes.count }}
                    </a>
                {% else %}
                    <a class="link right-margin" href="{% url 'feed:post_like' post.id %}">
                        Like: {{ post.likes.count }}
                    </a>
                {% endif %}
                <a class="link right-margin" href="{% url 'feed:post_detail' post.id %}">
                    Comments: {{ post.comments.count }}
                </a>
            </div>
        </div>

        {% if post.image %}
            <img class="post-pic" src="{{ post.image.url }}">
        {% endif %}

        <p class="post-body more-top-margin">{{ post.post_body }}</p>

        <div class="divider"></div>

        <p class="message-hint post-meta">{{ post.author }}</p>
        <p class="message-hint post-meta">{{ post.post_date }}</p>
        {% if request.user.is_authenticated and request.user == post.author or request.user.userprofile.user_level == 'admin' or request.user.userprofile.user_level == 'moderator' %}
            <a class="link right-margin" href="{% url 'feed:edit_post' post.id %}">
                Edit Post
            </a>
            <a class="link right-margin" href="{% url 'feed:delete_post' post.id %}">
                Delete Post
            </a>
        {% endif %}
    </div>
{% endfor %}

你知道吗视图.py地址:

class HomeView(LoginRequiredMixin,ListView):
    login_url = 'account_login'
    model = UserPost
    template_name = 'feed/userpost_list.html'
    context_object_name = 'posts'
    paginate_by = 25
    queryset = UserPost.objects.all()

型号:

class UserPost(models.Model):
    author = models.ForeignKey(User,related_name='userpost',null=True)
    post_date = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=150,blank=False)
    post_body = models.TextField(max_length=1000,blank=False)
    image = models.ImageField(upload_to='post_pics',blank=True)
    likes = models.ManyToManyField(User,blank=True,related_name='post_likes')

    class Meta:
        ordering = ['-post_date']

    def publish(self):
        self.save()

    def get_absolute_url(self):
        return reverse('index')

    def likes_as_flat_user_id_list(self):
        return self.likes.values_list('user_id', flat=True)

    def __str__(self):
        return self.title

Tags: marginselfrightdividurlmodelsrequest
1条回答
网友
1楼 · 发布于 2024-03-28 19:14:27

您可以向UserPost模型添加一个函数,将likes导出为一个平面列表,如下所示:

def likes_as_flat_user_id_list(self):
  return self.likes.values_list('id', flat=True)

不确定所需的确切代码,因为您尚未发布您的模型。你知道吗

然后,您应该能够在模板中执行类似的操作(同样,根据您的模型可能略有不同):

{% if request.user.id in post.likes_as_flat_user_id_list %}

编辑:

创建一个用户喜欢的帖子的平面列表,并将其作为模板参数传入,然后检查该列表中是否包含post id,可能会更有效。上面的代码为每个帖子创建了这样一个平面列表。你知道吗

相关问题 更多 >