搜索表单无法使用基于类的视图

时间:2013-02-12 03:01:58

标签: django-class-based-views

我正在尝试创建搜索表单,但它不起作用。这是我的代码:

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>

1 个答案:

答案 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)