Skip to content

Commit

Permalink
Fix error when empty search string is provided to list view
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Mar 3, 2017
1 parent 4410477 commit a839bcd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions smartmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,11 +645,11 @@ def derive_queryset(self, **kwargs):

# apply any filtering
search_fields = self.derive_search_fields()
if search_fields and 'search' in self.request.GET:
terms = self.request.GET['search'].split()
search_terms = self.request.GET.get('search', '').split(' ')
if search_fields and search_terms:

term_queries = []
for term in terms:
for term in search_terms:
field_queries = []
for field in search_fields:
field_queries.append(Q(**{field: term}))
Expand Down
4 changes: 4 additions & 0 deletions test_runner/blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ def test_list(self):
response = self.client.get(reverse('blog.post_list') + "?search=post")
self.assertEqual(list(response.context['post_list']), [post1, post4, post2, post3, self.post])

# empty search string should be ignored
response = self.client.get(reverse('blog.post_list') + "?search=")
self.assertEqual(list(response.context['post_list']), [post1, post4, post2, post3, self.post])

# change the format to json
response = self.client.get(reverse('blog.post_list') + "?_format=json")
self.assertEqual(json.loads(response.content.decode("utf-8")), [
Expand Down

0 comments on commit a839bcd

Please sign in to comment.