亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將項目上下文包含到 Post 模型中以激活 Django 中的 if 語句

將項目上下文包含到 Post 模型中以激活 Django 中的 if 語句

翻翻過去那場雪 2023-04-11 15:41:33
我正在嘗試將一個項目添加到列表視圖,UserPost 列表視圖已經有一個 Post 上下文。在我的項目中,用戶可以添加帖子和添加項目,每個都是具有不同模型的不同應用程序。因此,在我的 UserPost 列表視圖中,我的帖子循環與與之相關的特定用戶相關。我想做的是檢查這個 post.user 是否有一個由同一用戶過濾的項目,如果它確實存在,一個按鈕顯示出現在鏈接到另一個頁面的頁面中,該頁面包含與該用戶相關的項目列表。為了更具描述性,我想檢查Item并designer__post鏈接到此頁面{% url 'core:designer-posts' item.designer %}如果需要更多說明或代碼,我希望這能解決我的問題,請讓我知道添加它。我嘗試使用 Exists 子查詢 [Django-doc] 但我沒有成功完善它這是模型.pyclass Post(models.Model):    designer = models.ForeignKey(User, on_delete=models.CASCADE)    title = models.CharField(max_length=100)這是views.pyclass 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.pyclass 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'),]
查看完整描述

2 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

首先,您需要通過覆蓋get_context_data(...)方法來傳遞一個上下文變量,它決定用戶Items是否有


class UserPostListView(ListView):

    # rest of your code

    def get_context_data(self, *args, **kwargs):

        context = super().get_context_data(*args, **kwargs)

        has_items = Item.objects.filter(designer__username=self.kwargs['username']).exists()

        context['has_items'] = has_items

        return context

然后在你的模板中,使用這個has_items變量,


{% if has_items %}

    <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獲取用戶名


查看完整回答
反對 回復 2023-04-11
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

在 {% url %} item.designer 中傳遞一個用戶對象,你必須像這樣提供 id

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


查看完整回答
反對 回復 2023-04-11
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號