I use haystack with whoosh, and django 1.3. In my url I have:

url(r'^search/', include('haystack.urls')),

I created custom template in app: search/seach.html:

{% if page.object_list %}                                                        

{% autopaginate page.object_list 3 %}
{% for arg in page.object_list %}
...
{% endfor%}
{% paginate %}

My search works fine! Also rendered pagination (with paginate templatetag) is correct. But when I try to go to next page, I get this error:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/search/?page=2&q=Aktualno%C5%9B%C4%87

Can You help me? Whats wrong?

In Django 1.8.4 you just have to replace page for page_obj in your template.

Example:

{% for arg in page_obj.object_list %}

I made django-haystack and django-pagination work by using a custom SearchView that extends the SearchView of django-haystack. Just comment out or remove the (paginator, page) = self.build_page() and the page and paginator context variable.

Below is my custom SearchView.

class SearchView(SearchView):

def create_response(self):
    """
    Generates the actual HttpResponse to send back to the user.
    """
    # (paginator, page) = self.build_page()

    context = {
        'query': self.query,
        'form': self.form,
        # 'page': page,
        # 'paginator': paginator,
        'suggestion': None,
        'results': self.results
    }

    if self.results and hasattr(self.results, 'query') and self.results.query.backend.include_spelling:
        context['suggestion'] = self.form.get_suggestion()

    context.update(self.extra_context())
    return render_to_response(self.template, context, context_instance=self.context_class(self.request))

and in the template paginate using the results context variable:

  {% autopaginate results 2 %}
  {% for result in results %}
      {{ result.attribute }}
  {% endfor %}
  {% paginate %}

Thats it :)

The haystack default SearchView's page doesn't support django-pagination. If you want to use django-pagination, you can Inherit the SearchView and ad a new context variable to store the search result. And then you can use this context variable to pagination.

def create_response(self):
    (paginator, page) = self.build_page()        
    context = {
        'query': self.query,
        'form': self.form,
        'page': page,
        'results': self.results,
        'paginator': paginator,
        'suggestion': None,
        }

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.