Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when empty search string is provided to list view #94

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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