context_object_name = 'item_list' template_name = 'krop_view.html' model = Item def get_queryset(self): user = self.request.user #gets current user object krop = get_object_or_404(Krop, owner=user) return Item.objects.filter(krop=krop) def get_context_data(self, **kwargs): context = super(KropListView, self).get_context_data(**kwargs) user = self.request.user context['krop'] = Krop.objects.filter(owner=user) return context從技術上講,“Krop”是一家商店,我可以獲得當前用戶擁有的商店的所有物品。我還想訪問 Krop 模型中的數據,因此我傳入了與當前用戶相關的 krop 上下文數據。如何在模板中訪問此上下文數據?我已經嘗試并查找了獲取特定變量的方法,例如:{{ item_list我想從兩個不同的表訪問數據,但對于一個頁面。請和謝謝你!!!
1 回答

哈士奇WWW
TA貢獻1799條經驗 獲得超6個贊
krop 變量是包含多個項的查詢集。您需要遍歷它們,您將能夠訪問所需的字段。
{% for item in krop %} Owner: {{ item.owner }} {% endfor %}
添加回答
舉報
0/150
提交
取消