【问题标题】:How to do product sorting when we search for certain product in Django?当我们在 Django 中搜索某些产品时如何进行产品排序?
【发布时间】:2020-06-03 06:06:40
【问题描述】:

views.py:-

def search(request):
global pro
global products
if not request.GET.get('price_filter') == '1' or request.GET.get('price_filter') == '2':
    q = request.GET.get("q")
    products = Product.objects.filter(active=True, name__icontains=q)
    categories = Category.objects.filter(active=True)
    brands = Brand.objects.filter(active=True)
    context = {"products": products,
               "categories": categories,
                "brands": brands,
                "title": q + " - search"}
    return render(request, "shop/home.html", context)
pro = products

if request.GET.get('price_filter') == '1':
    products = pro.order_by('price')
    categories = Category.objects.filter(active=True)
    brands = Brand.objects.filter(active=True)
    context = {"products": products,
               "categories": categories,
               "brands": brands}
    return render(request, "shop/home.html", context)

elif request.GET.get('price_filter') == '2':
    products = pro.order_by('-price')
    categories = Category.objects.filter(active=True)
    brands = Brand.objects.filter(active=True)
    context = {"products": products,
               "categories": categories,
               "brands": brands}
    return render(request, "shop/home.html", context)

在 HTML 中:-

<form method='get' action='#' style="margin-top:-20px; margin-left: 8px;">
        <input class="btn btn-outline-dark" type="checkbox" value="1" name="price_filter"/>Low to High

        <input class="btn btn-outline-dark" type="checkbox" value="2" name="price_filter"/>High to Low
        <button class="btn" type="submit" value="Sort">Sort</button>
    </form>

使用该搜索我们可以从低到高排序,但是当我选择从高到低时,它会显示某些错误:-

Cannot use None as a query value

我知道这不是正确的方法,但请帮我解决这个问题,或指导我正确的排序方法。

【问题讨论】:

  • 尝试打印q
  • 在你的最后一个 elif 块中打印 pro 条件为 ==2 它得到任何值让我知道
  • @l.b.vasoya 它没有显示,没有任何价值。

标签: python django django-views django-filter


【解决方案1】:

此行出现此错误:

if not request.GET.get('price_filter') == '1' or request.GET.get('price_filter') == '2':
                                                ^^^

它指出如果没有request.GET.get('price_filter') 的值并且有request.GET.get('price_filter') 的值。我认为这里应该是这样的 not 子句:

if not request.GET.get('price_filter') == '1' or not request.GET.get('price_filter') == '2':
                                                 ^^^

原因是,您的模板中没有 q 的输入字段。

此外,您可以将查询字符串值存储在变量中以使这段代码更简洁:

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

if price_filter not in ["1", "2"]:
    q = request.GET.get("q")
    # rest of the code
elif price_filter == "1":
    # more code
elif price_filter == "2":
    # more code

【讨论】:

【解决方案2】:

你应该尝试我给出的代码

def search(request):
    price_filter = request.GET.get('price_filter',None) 
    context = {}
    if price_filter not in '12': 
        q = request.GET.get("q")
        products = Product.objects.filter(active=True, name__icontains=q)
        context = {"title": q + " - search"}

    if price_filter == '1':
        pro = Product.objects.filter(active=True, name__icontains=q) if request.GET.get("q",None) else  Product.objects.filter(active=True)
        products = pro.order_by('price')

    elif price_filter == '2':
        pro = Product.objects.filter(active=True, name__icontains=q) if request.GET.get("q",None) else  Product.objects.filter(active=True)
        products = pro.order_by('-price')



    categories = Category.objects.filter(active=True)
    brands = Brand.objects.filter(active=True)               
    context =context.update({"products": products,
                   "categories": categories,
                    "brands": brands})               
    return render(request, "shop/home.html", context)

如果遇到或使用这些东西有任何问题,请告诉我

【讨论】:

    【解决方案3】:

    在视图中我改变了这个,现在它工作得很好:-

    views.py

    def search(request):
    global pro
    global products
    if request.GET.get('price_filter') not in ["1", "2"]:
        q = request.GET.get("q")
        products = Product.objects.filter(active=True, name__icontains=q)
        categories = Category.objects.filter(active=True)
        brands = Brand.objects.filter(active=True)
        context = {"products": products,
                   "categories": categories,
                    "brands": brands,
                    "title": q + " - search"}
        return render(request, "shop/home.html", context)
    pro = products
    
    if request.GET.get('price_filter') == '1':
        products = pro.order_by('price')
        categories = Category.objects.filter(active=True)
        brands = Brand.objects.filter(active=True)
        context = {"products": products,
                   "categories": categories,
                   "brands": brands}
        return render(request, "shop/home.html", context)
    
    elif request.GET.get('price_filter') == '2':
        products = pro.order_by('-price')
        categories = Category.objects.filter(active=True)
        brands = Brand.objects.filter(active=True)
        context = {"products": products,
                   "categories": categories,
                   "brands": brands}
        return render(request, "shop/home.html", context)
    

    【讨论】:

      猜你喜欢
      • 2015-08-22
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多