分配前在/ products / search /本地变量'query'处引用了UnboundLocalError

时间:2019-07-25 23:29:43

标签: python django django-models django-forms

我有这个问题

  

分配前在/ products / search局部变量'query'处出现 UnboundLocalError

/关于此代码的帮助,正在使用标题和类别搜索产品

def search(request):
    try:
        q = request.GET.get('q', '')
    except:
        q = False
    if q:
        query = q

    product_queryset = Product.objects.filter(
        Q(name__icontains=q)|
        Q(description__icontains=q)
    )
    category_queryset = Category.objects.filter(
        Q(title__icontains=q)|
        Q(description__icontains=q)
    )
    results = list(chain(product_queryset,category_queryset))
    context = {
        'query':query,
        'product_queryset':product_queryset,
        'category_queryset':category_queryset,
        'results':results,
    }

    return render(request,"products/search.html", context)

2 个答案:

答案 0 :(得分:0)

就像塞尔丘克说的那样,如果query为None / False,则不会定义q。试试:

def search(request):
    try:
        q = request.GET.get('q', '')
    except:
        q = False
    if q:
        query = q
    else:
        query = None

    product_queryset = Product.objects.filter(
        Q(name__icontains=q)|
        Q(description__icontains=q)
    )
    category_queryset = Category.objects.filter(
        Q(title__icontains=q)|
        Q(description__icontains=q)
    )
    results = list(chain(product_queryset,category_queryset))
    context = {
        'query':query,
        'product_queryset':product_queryset,
        'category_queryset':category_queryset,
        'results':results,
    }

    return render(request,"products/search.html", context)

我们不确定query应该是什么,或者您如何使用它。将其设置为None应该可以解决UnboundLocalError。

我也没有看到您需要将q = request.GET.get('q', '')包装在try / except块中的任何原因。如果未定义getq将返回''。您可以将代码简化为:

def search(request):
    q = request.GET.get('q', '')

    product_queryset = Product.objects.filter(
        Q(name__icontains=q)|
        Q(description__icontains=q)
    )
    category_queryset = Category.objects.filter(
        Q(title__icontains=q)|
        Q(description__icontains=q)
    )
    results = list(chain(product_queryset,category_queryset))
    context = {
        'query': q,
        'product_queryset':product_queryset,
        'category_queryset':category_queryset,
        'results':results,
    }

    return render(request,"products/search.html", context)

模板表格

    <form method="get" action="{% url 'products:search' %}">
        <button class="flex-c-m trans-04">
            <i class="zmdi zmdi-search"></i>
        </button>
        <input class="plh3" type="text" placeholder="Search..." name="q">
    </form>

答案 1 :(得分:0)

您无需像使用try / catch块那样简单地更改视图:

 def search(request):
      q = request.GET.get('q')
      if q:

            product_queryset = Product.objects.filter(
            Q(name__icontains=q)|
            Q(description__icontains=q)
        )
        category_queryset = Category.objects.filter(
            Q(title__icontains=q)|
            Q(description__icontains=q)
        )
        results = list(chain(product_queryset,category_queryset))
        context = {
            'query': q,
            'product_queryset':product_queryset,
            'category_queryset':category_queryset,
            'results':results,
        }

        return render(request,"products/search.html", context)
      else:
         messages.info(request,'No results found for {}'.format(q))