1

我尝试基于 ListView 为我的类编写 get 方法,我想从模板中的表单获取请求,并从请求中返回带有过滤器的模型。这是我的代码的一部分:

class SearchListView(ListView):
    context_object_name = 'projects_list'
    template_name = 'projects/search.html'
    paginate_by = 10

    def get(self, request, *args, **kwargs):
        do smth??
        return self.render_to_response(??)

我的表格:

<form class="well form-search" action="/search/" method="get">
    <input type="text" class="input-medium search-query" name="q">
    <button type="submit" class="btn">Search</button>
</form>

请给我举个例子。

4

2 回答 2

4

无需重写get方法。

class SearchListView(ListView):
    context_object_name = 'projects_list'
    template_name = 'projects/search.html'
    paginate_by = 10

    def get_queryset(self):
        query = self.request.GET.get('q')
        return Model.objects.filter(title=q)
于 2012-04-07T18:50:10.920 回答
2

您可以像这样使用 super() :

class SearchListView(ListView):
    ...

    def get(self, request, *args, **kwargs):
        do smth??
        return super(SearchListView, self).get(request, *args, **kwargs)
于 2016-01-18T16:17:56.363 回答