同时搜索标题和价格时出错

问题描述 投票:0回答:1

我有一个过滤器表单,可以在其中按类别,标题和价格过滤内容。我可以同时按类别和价格或类别和标题搜索项目,但是当我尝试使用价格和标题进行过滤时出现错误。

这里是错误,我是按价格和标题过滤后得到的:

FieldError at /

 Cannot resolve keyword 'current_price' into field. Choices are: category, description, discount_price, id, image, label, orderitem, price, slug, title

views.py

def HomeView(request):
  item_list = Item.objects.all()
  item_list = item_list.annotate(current_price=Coalesce('discount_price', 'price'))

  category_list = Category.objects.all()
  query = request.GET.get('q')

  if query:
      item_list = Item.objects.filter(title__icontains=query)

  cat = request.GET.get('cat')
  if cat:
      item_list = item_list.filter(category__pk=cat)

  price_from = request.GET.get('price_from')
  price_to = request.GET.get('price_to')

  if price_from:
      item_list = item_list.filter(current_price__gte=price_from)

  if price_to:
      item_list = item_list.filter(current_price__lte=price_to)

  paginator = Paginator(item_list, 10)

  page = request.GET.get('page')

  try:
      items = paginator.page(page)
  except PageNotAnInteger:
      items = paginator.page(1)
  except EmptyPage:
      items = paginator.page(paginator.num_pages)

  context = {
      'items': items,
      'category': category_list
  }
  return render(request, "home.html", context)

html:

        <form method="GET" action=".">
        <div class="form-group col-md-4">
            <label for="category">Category</label>
            <select id="cat" class="form-control" name="cat">
                <option value="" selected>Choose...</option>
                <option value="" href="/home">All</option>
                {% for cat in category %}
                <option value="{{ cat.pk }}">{{ cat }}</option>
                {% endfor %}
            </select>

        </div>
        <div class="input-group">
            <input class="form-control py-2 border-right-0 border" type="search" name="q" placeholder="Brand..">
            <span class="input-group-append">
                <div class="input-group-text bg-transparent">
                    <i class="fa fa-search"></i>
                </div>
            </span>
        </div>
        <div class="input-group">
            <input class="form-control py-2 border-right-0 border" type="search" name="price_from"
                placeholder="Price from">
            <span class="input-group-append">
                <div class="input-group-text bg-transparent">
                    <i class="fa fa-search"></i>
                </div>
            </span>
        </div>
        <div class="input-group">
            <input class="form-control py-2 border-right-0 border" type="search" name="price_to"
                placeholder="Price to">
            <span class="input-group-append">
                <div class="input-group-text bg-transparent">
                    <i class="fa fa-search"></i>
                </div>
            </span>
        </div>
        <button type="submit" class="btn btn-primary">Search</button>
    </form>
python django
1个回答
0
投票

您的错误可能是由引起的

if query:
       item_list = Item.objects.filter(title__icontains=query)

您要覆盖具有item_list批注的current_price的位置。

尝试过滤item_list而不是Item.objects

© www.soinside.com 2019 - 2024. All rights reserved.