Django: Search form in Class Based ListView

前端 未结 7 2159
遥遥无期
遥遥无期 2020-12-04 13:22

I am trying to realize a Class Based ListView which displays a selection of a table set. If the site is requested the first time, the dataset should be displaye

7条回答
  •  半阙折子戏
    2020-12-04 13:58

    This is similar to @jasisz 's approach, but simpler.

    class ProfileList(ListView):
        template_name = 'your_template.html'
        model = Profile
    
        def get_queryset(self):
            query = self.request.GET.get('q')
            if query:
                object_list = self.model.objects.filter(name__icontains=query)
            else:
                object_list = self.model.objects.none()
            return object_list
    

    Then all you have to do on the html template is:

提交回复
热议问题