Django中的分页后没有遵循的过滤器

时间:2019-05-12 18:14:18

标签: python django pagination django-templates

我正在尝试在Django项目中设置分页,但是我找不到一种使过滤器(在我的情况下为ptags)跟随下一页的方法。

Ps;我使用带构面的Django-haystack进行过滤。

我有一个自定义表格。py

from haystack.forms import FacetedSearchForm


class FacetedProductSearchForm(FacetedSearchForm):

    def __init__(self, *args, **kwargs):
        data = dict(kwargs.get("data", []))
        self.ptag = data.get('ptags', [])
        super(FacetedProductSearchForm, self).__init__(*args, **kwargs)

    def search(self):
        sqs = super(FacetedProductSearchForm, self).search()

        if self.ptag:
            query = None
            for ptags in self.ptag:
                if query:
                    query += u' OR '
                else:
                    query = u''
                query += u'"%s"' % sqs.query.clean(ptags)
            sqs = sqs.narrow(u'ptags_exact:%s' % query)

        return sqs

我通过了我的观点

class FacetedSearchView(BaseFacetedSearchView):

    form_class = FacetedProductSearchForm
    facet_fields = ['ptags']
    template_name = 'search_result.html'
    paginate_by = 3
    context_object_name = 'object_list'

这是我完整的search_result.html:

  <div>
{% if  page_obj.object_list %}
                <ol class="row top20" id="my_list">

        {% for result in page_obj.object_list %}

<li class="list_item">
   <div class="showcase col-sm-6 col-md-4">

   <div class="matching_score"></div>

           <a href="{{ result.object.get_absolute_url }}">
              <h3>{{result.object.title}}</h3>
                      <h5>{{ result.object.destination }}</h5>
              <img src="{{ result.object.image }}" class="img-responsive">
           </a>

<div class="text-center textio">
    <ul class="tagslist">
      <li class="listi">
{% for tags in result.object.ptags.names %}

        <span class="label label-info"># {{ tags }}</span>

{% endfor %}
      </li>
    </ul>
</div>

   </div>
</li>
                  {% endfor %}
                </ol>
            </div>

通过在html的底部添加以下内容,我可以将作为目的地的查询传递给下一页:

            {% if is_paginated %}
              <ul class="pagination pull-right">
                {% if page_obj.has_previous %}
                  <li><a href="?q={{ query }}&page={{ page_obj.previous_page_number }}"><i class="fas fa-angle-left"></i>Previous page</a></li>
                {% else %}
                  <li class="disabled"><span><i class="fas fa-angle-left"></i>Previous page</span></li>
                {% endif %}
                {% if page_obj.has_next %}
                  <li><a href="?q={{ query }}&page={{ page_obj.next_page_number }}">See more results<i class="fas fa-angle-right"></i></a></li>
                {% else %}
                  <li class="disabled"><span>See more results<i class="fas fa-angle-right"></i></span></li>
                {% endif %}
              </ul>
            {% endif %}
        {% else %}
        <p> Sorry, no result found for the search term  <strong>{{query}} </strong></p>
    {% endif %}

但是,如果我尝试通过这样的&ptags在分页代码中添加ptags字段,则:

{% if is_paginated %}
              <ul class="pagination pull-right">
                {% if page_obj.has_previous %}
                  <li><a href="?q={{ query }}&ptags={{page_obj.ptags.name}}&page={{ page_obj.previous_page_number }}"><i class="fas fa-angle-left"></i>Previous page</a></li>
                {% else %}
                  <li class="disabled"><span><i class="fas fa-angle-left"></i>Previous page</span></li>
                {% endif %}
                {% if page_obj.has_next %}
                  <li><a href="?q={{ query }}&ptags={{page_obj.ptags.name}}&page={{ page_obj.next_page_number }}">See more results<i class="fas fa-angle-right"></i></a></li>
                {% else %}
                  <li class="disabled"><span>See more results<i class="fas fa-angle-right"></i></span></li>
                {% endif %}
              </ul>
            {% endif %}
        {% else %}
        <p> Sorry, no result found for the search term  <strong>{{query}} </strong></p>
    {% endif %}

当我转到第2页时,出现此错误,并且所选的复选框过滤器(ptags)没有显示:

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/search/?q=las%20vegas&ptags=&page=2
Raised by:  search.views.FacetedSearchView

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的模板分页代码正在删除多余的过滤器参数,请使用此-

在您的my_tags.py中添加以下模板标记:

@register.simple_tag(name='url_replace')
def url_replace(request, field, value):
    d = request.GET.copy()
    d[field] = value
    return d.urlencode()

以-

分页
{% if is_paginated %}
      <br>
      <div class="row">
        <div class="col-12">
        <ul class="pagination float-right">
          {% if page_obj.has_previous %}
            <li class="page-item"><a class="page-link" href="?{% url_replace request 'page' page_obj.previous_page_number %}">&laquo;</a></li>
          {% else %}
            <li class="page-item disabled"><span class="page-link">&laquo;</span></li>
          {% endif %}
          {% for i in paginator.page_range %}
            {% if page_obj.number == i %}
              <li class="active page-item"><span class="page-link">{{ i }} <span class="sr-only">(current)</span></span></li>
            {% else %}
              <li class="page-item"><a class="page-link" href="?{% url_replace request 'page' i %}">{{ i }}</a></li>
            {% endif %}
          {% endfor %}
          {% if page_obj.has_next %}
            <li class="page-item"><a class="page-link" href="?{% url_replace request 'page' page_obj.next_page_number %}">&raquo;</a></li>
          {% else %}
            <li class="page-item disabled"><span class="page-link">&raquo;</span></li>
          {% endif %}
        </ul>
      </div>
    </div>
 {% endif %}