我在所有项目中使用Q查找,但这是我遇到的第一个。首先,查找按预期工作,但刷新页面时查找字段仍显示<< MISSING VARIABLE "request.GET.q" >>
。如果查找不起作用,我可能会发现问题但我在这个问题上有点难过。下面是视图和模板表单。谢谢。
查看
def product_list(request):
queryset_list = Product.objects.prefetch_related('images').select_subclasses()
query = request.GET.get("q")
if query:
queryset_list = queryset_list.filter(
Q(name__icontains=query)
).distinct()
paginator = Paginator(queryset_list, 10)
page_request_var = "page"
page = request.GET.get(page_request_var)
try:
queryset = paginator.page(page)
except PageNotAnInteger:
queryset = paginator.page(1)
except EmptyPage:
queryset = paginator.page(paginator.num_pages)
form = forms.ProductClassForm(request.POST or None)
if form.is_valid():
return redirect('dashboard:product-add')
ctx = {
'form': form,
"object_list": queryset,
"title": "Products",
"page_request_var": page_request_var,
}
return TemplateResponse(request, 'dashboard/product/list.html', ctx)
查找表单
<form method='GET' action=''>
<label for="full-text">Search Product List</label>
<input type="text" placeholder="Enter Keywords and Press Enter" class="search-input" name="q" value='{{ request.GET.q }}'>
</form>