我正在尝试创建搜索表单,但它不起作用。这是我的代码:
class LocationSearchMixin(object):
def get_queryset(self):
q = self.request.GET.get('q')
if q is None:
return queryset
class StoreListView(LocationSearchMixin, ListView):
model = Store
<form action="" method="GET">
<input type="text" name="q" />
<button type="submit">search</button>
</form>
答案 0 :(得分:0)
您必须在LocationSearchMixin中添加一些内容:
class LocationSearchMixin(object):
def get_queryset(self):
queryset = super(LocationSearchMixin, self).get_queryset()
q = self.request.GET.get('q')
if q is None:
return queryset
return queryset.filter(location__istartswith=q)