猿问

将项目上下文包含到 Post 模型中以激活 Django 中的 if 语句

我正在尝试将一个项目添加到列表视图,UserPost 列表视图已经有一个 Post 上下文。


在我的项目中,用户可以添加帖子和添加项目,每个都是具有不同模型的不同应用程序。


因此,在我的 UserPost 列表视图中,我的帖子循环与与之相关的特定用户相关。


我想做的是检查这个 post.user 是否有一个由同一用户过滤的项目,如果它确实存在,一个按钮显示出现在链接到另一个页面的页面中,该页面包含与该用户相关的项目列表。为了更具描述性,我想检查Item并designer__post链接到此页面{% url 'core:designer-posts' item.designer %}


如果需要更多说明或代码,我希望这能解决我的问题,请让我知道添加它。


我尝试使用 Exists 子查询 [Django-doc] 但我没有成功完善它


这是模型.py


class Post(models.Model):

    designer = models.ForeignKey(User, on_delete=models.CASCADE)

    title = models.CharField(max_length=100)

这是views.py


class UserPostListView(ListView):

    model = Post

    template_name = "user_posts.html"

    context_object_name = 'posts'

    queryset = Post.objects.filter(admin_approved=True)

    paginate_by = 6


    def get_queryset(self):

        user = get_object_or_404(User, username=self.kwargs.get('username'))

        return Post.objects.filter(designer=user, admin_approved=True).order_by('-date_posted')

这是模板 user_posts.html


{% if item %}

 <a class="primary btn-lg" href="{% url 'core:designer-posts' item.designer %}" role="button">Go to items</a>

{% else %}

  <a href="{% url 'core:designer-posts' item.designer %}">

    <button type="button" class="btn btn-primary btn-lg btn-block">Go to items</button>

  </a>   

{% endif %}

这是项目 models.py


class Item(models.Model):

    designer = models.ForeignKey(

        User, on_delete=models.CASCADE)

    title = models.CharField(max_length=100)

这是我试图从用户帖子视图链接到的 designerlist views.py(如果可用)


class DesignerPostListView(ListView):

    model = Item

    template_name = "designer_posts.html"

    context_object_name = 'items'

    paginate_by = 6


    def get_queryset(self):

        user = get_object_or_404(User, username=self.kwargs.get('username'))

        return Item.objects.filter(designer=user).order_by('-timestamp')

以下是与帖子模型相关的视图


app_name = 'score'


urlpatterns = [

    path('', PostListView.as_view(), name='score'),

    path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),

]



翻翻过去那场雪
浏览 115回答 2
2回答

一只斗牛犬

首先,您需要通过覆盖get_context_data(...)方法来传递一个上下文变量,它决定用户Items是否有class UserPostListView(ListView):&nbsp; &nbsp; # rest of your code&nbsp; &nbsp; def get_context_data(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; context = super().get_context_data(*args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; has_items = Item.objects.filter(designer__username=self.kwargs['username']).exists()&nbsp; &nbsp; &nbsp; &nbsp; context['has_items'] = has_items&nbsp; &nbsp; &nbsp; &nbsp; return context然后在你的模板中,使用这个has_items变量,{% if has_items %}&nbsp; &nbsp; <a class="primary btn-lg" href="{% url 'core:designer-posts' username=view.kwargs.username %}" role="button">Go to items</a>{% endif %}另外,您没有将 传递username给url标签,应该是{% url 'core:designer-posts' username=view.kwargs.username %}在这里,我曾经从 URL 中view.kwargs.username获取用户名

BIG阳

在 {% url %} item.designer 中传递一个用户对象,你必须像这样提供 id&nbsp;<a&nbsp;href="{%&nbsp;url&nbsp;'core:designer-posts'&nbsp;item.designer.id&nbsp;%}">
随时随地看视频慕课网APP

相关分类

Python
我要回答