如何基于ListView编写类的get方法

时间:2012-04-07 18:41:52

标签: django django-views

我尝试基于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>

Plz,给我一些例子。

2 个答案:

答案 0 :(得分:2)

无需重写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)

答案 1 :(得分:2)

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

class SearchListView(ListView):
    ...

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