是否有一种简单的方法可以删除模板中的查询集对象,仅打印不带对象的产品名称 它打印什么
class SellerAccountMixin(object):
products = []
def get_products(self):
account = self.get_account()
products = Product.objects.filter(seller=account)
self.products = products
return products
class SellerDashboard(SellerAccountMixin,FormMixin, View):
def get(self, request, *args, **kwargs):
context["products"] = self.get_products()
return render(request, "sellers/dashboard.html", context)
模板
{% if products %}
<div class='pull-left col-sidebar '>
{{ products }}
</div>
答案 0 :(得分:0)
您应该在products
上迭代,例如:
{% if products %}
<div class='pull-left col-sidebar '>
{% for product in products %}{{ product.name }} {% endfor %}
</div>
{% endif %}
.name
可能是一个不同的字段。它应该是您要渲染的对象。