gpt4 book ai didi

python - Django QuerySet 对象没有属性 'objects

转载 作者:太空狗 更新时间:2023-10-30 00:43:12 28 4
gpt4 key购买 nike

我对类别中的展示产品有疑问。 (产品已展示,但当我点击类别(t 恤)时,我在/man/tshirt/遇到了这个问题 AttributeError'QuerySet' 对象没有属性 'objects

View .py

def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(section='man', available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.objects.filter(category=category)
return render(request,
'shop/product/list.html',
{'category': category,
'categories': categories,
'products': products})

网址.py

 urlpatterns = [
url(r'^$', views.main, name='main'),
url(r'^man/$', views.product_list, name='product_list'),
url(r'^man/(?P<category_slug>[-\w]+)/$',
views.product_list,
name='product_list_by_category'),


]

模型.py

class Category(models.Model):
name = models.CharField(max_length=200,
db_index=True)
slug = models.SlugField(max_length=200,
db_index=True)

class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse('shop:product_list_by_category',
args=[self.slug])

列表.html

{% extends "shop/base.html" %}
{% load static %}

{% block title %}VPW{% endblock %}

{% block content %}
<ul>
{% for c in categories %}
<li>
<a href="{{ c.get_absolute_url }}">{{c.name}}</a>
</li>
{% endfor %}
</ul>

{% for product in products %}
{{ product.name }}
{% endfor %}
{% endblock %}

最佳答案

你应该改变

products = products.objects.filter(category=category)

products = products.filter(category=category)

简而言之,您已经在 objects 级别查询了数据,因此 .objects 标识符在代码中此时不再需要或有效。

您可以找到更多信息 here .

关于python - Django QuerySet 对象没有属性 'objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055283/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com