我正在尝试对在线商店中的产品进行简单的搜索。我看了教程,并在这里阅读了问题。我试图做到这一点。没有成功。我做错了什么? view.py
this.oipChallengeForm = <FormGroup>this.formBuilder.group({
startDate: ['', [Validators.required]]
product \ list.html
from django.shortcuts import render, get_object_or_404, render_to_response
from django.db.models import Q
from cart.forms import CartAddProductForm
from .models import Category, Product
# Product page
def ProductList(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
query = request.GET.get('q')
if query:
return Product.objects.filter(title_icontains=query)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'product/list.html', {
'category': category,
'categories': categories,
'products': products
})
def ProductDetail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
return render(request, 'product/detail.html', {'product': product})
def ProductDetail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
cart_product_form = CartAddProductForm()
return render(request, 'product/detail.html',
{'product': product,
'cart_product_form': cart_product_form})
答案 0 :(得分:1)
def ProductList(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
query = request.GET.get('query') //as your input field name is query and not q
if query:
products = Product.objects.filter(title__icontains=query) // __ not _
return render(request, 'product/list.html', {
'products': products
})
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
return render(request, 'product/list.html', {
'category': category,
'categories': categories,
'products': products
})
将视图更改为此