Django-haystack和django-pagination不能一起工作? [英] Django-haystack and django-pagination not working together?

查看:177
本文介绍了Django-haystack和django-pagination不能一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用haystack与whoosh,和django 1.3。在我的网址中,我有:

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

我在app中创建了自定义模板:search / seach.html:

  {%if page.object_list%} 

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

我的搜索工作正常!还有分页(与分页模板)是正确的。但是当我尝试去下一页时,我收到这个错误:

 找不到页面(404)
请求方法:GET
请求URL:http://127.0.0.1:8000/search/?page=2&q=Aktualno%C5%9B%C4%87

你能帮我吗?有什么问题?

解决方案

我通过使用自定义的SearchView来扩展了django-haystack和django-pagination的SearchView,草垛。只需注释或删除(paginator,page)= self.build_page()以及页面和分页上下文变量。



以下是我的自定义SearchView。

  class SearchView(SearchView):

def create_response

生成实际的HttpResponse发送给用户

#(paginator,page)= self.build_page()

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

如果self.results和hasattr(self.results, 'query')和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(s​​elf.request))

并在模板分页中使用结果上下文变量: p>

  {%autopaginate results 2%} 
{%用于结果%}
{{result.attribute }}
{%endfor%}
{%paginate%}

它:)


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?

解决方案

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

这篇关于Django-haystack和django-pagination不能一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆