0

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?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
user979432
  • 11
  • 4

3 Answers3

2

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 :)

John Kenn
  • 1,607
  • 2
  • 18
  • 31
2

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 %}
Rhemzo
  • 73
  • 6
1

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,
        }
risent
  • 136
  • 1
  • 4