我正在使用Python,Django 2.1和PostgreSQL制作一个电子商务应用程序。当我单击某个类别以显示特定于该类别的产品时,它表明我有数据错误。显然,在URL中,当应该有一个整数时有一个字符串。我不明白请帮助
我尝试了许多不同的方法,但似乎都没有效果。
我的网址格式:
path('products/phones/', product_views.CategoryDetailView.as_view(template_name='products/category_details/phones.html'), name='phones'),
path('products/laptops/', product_views.CategoryDetailView.as_view(template_name='products/category_details/laptops.html'), name='laptops'),
path('products/desktops/', product_views.CategoryDetailView.as_view(template_name='products/category_details/desktops.html'), name='desktops'),
path('products/keyboards/', product_views.CategoryDetailView.as_view(template_name='products/category_details/keyboards.html'), name='keyboards'),
path('products/mice-and-mouse-pads/', product_views.CategoryDetailView.as_view(template_name='products/category_details/mice.html'), name='mice'),
path('products/headsets/', product_views.CategoryDetailView.as_view(template_name='products/category_details/headsets.html'), name='headsets'),
path('products/printers-scanners-and-fax/', product_views.CategoryDetailView.as_view(template_name='products/category_details/printers.html'), name='printers'),
path('products/consoles/', product_views.CategoryDetailView.as_view(template_name='products/category_details/consoles.html'), name='consoles'),
path('products/misc/', product_views.CategoryDetailView.as_view(template_name='products/category_details/misc.html'), name='misc'),
收到的错误是:
django.db.utils.DataError: invalid input syntax for integer: "Phones"
LINE 1: ...s_product" WHERE "products_product"."category_id" = 'Phones'
我的观点
model = Category
queryset = Category.objects.all()
template_name = 'products/category_detail.html'
context_object_name = 'categories'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Query Sets
context['phones'] = Product.objects.filter(category='Phones')
context['laptops'] = Product.objects.filter(category='Laptops')
context['total_laptops'] = len(Product.objects.filter(category='Laptops'))
context['desktops'] = Product.objects.filter(category='Desktops')
context['total_desktops'] = len(Product.objects.filter(category='Desktops'))
context['keyboards'] = Product.objects.filter(category='Keyboards')
context['total_keyboards'] = len(Product.objects.filter(category='Keyboards'))
context['mice'] = Product.objects.filter(category='Mice and Mouse Pads')
context['total_mice'] = len(Product.objects.filter(category='Mice and Mouse Pads'))
context['printers'] = Product.objects.filter(category='Printers, Scanners, and Fax Machines')
context['total_printers'] = len(Product.objects.filter(category='Printers, Scanners, and Fax Machines'))
context['consoles'] = Product.objects.filter(category='Consoles')
context['total_consoles'] = len(Product.objects.filter(category='Consoles'))
context['miscellaneous'] = Product.objects.filter(category='Miscellaneous')
context['total_miscellaneous'] = len(Product.objects.filter(category='Miscelaneous'))
return context