Django: Search form in Class Based ListView

前端 未结 7 2058
遥遥无期
遥遥无期 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 14:21

    I think that the error you are getting is because your form doesn't require the name field. So, although the form is valid, the cleaned_data for your name field is empty.

    These could be the problematic lines:

    if form.is_valid():
        self.show_results = True
        self.profiles = Profile.objects.filter(name__icontains=form.cleaned_data['name'])
    

    If I were you, I would try changing the line:

    self.profiles = Profile.objects.filter(name__icontains=form.cleaned_data['name'])
    

    to this:

    self.profiles = Profile.objects.none()
    

    If you stop receiving errors (and your template receives an empty object_list), the problem you have is what I said before: name field not required.

    Let us know if this doesn't work!

    0 讨论(0)
提交回复
热议问题