toastdriven/django-haystack — Issue #463: Negative index on pagination
Author: sgoll
Created: 2011-12-21 07:16:05+00:00
State: open

Current master has the following problem. When pagination is active and a page with a non-positive index is requested manually, i.e. for instance `?page=0` or `?page=-1`, an exception is thrown in `haystack/query.py`, method `__getitem__()`: "Negative indexing is not supported".

This is due to an unchecked queryset slice in `haystack/views.py`, method `build_page()`. Ideally, we would use Django paginator's `validate_number()` method, but since we need `page_no` before actually creating the `Paginator` instance, we must do the check manually.

I suggest the following patch:

```
diff --git a/haystack/views.py b/haystack/views.py
index 863a790..428297e 100644
--- a/haystack/views.py
+++ b/haystack/views.py
@@ -100,6 +100,9 @@ class SearchView(object):
except (TypeError, ValueError):
raise Http404

+ if page_no < 1:
+ raise Http404
+
start_offset = (page_no - 1) * self.results_per_page
self.results[start_offset:start_offset + self.results_per_page]
```