Django分页在我的搜索结果页面中不起作用

时间:2019-04-08 03:35:58

标签: html django pagination

我只是尝试为搜索结果页面添加分页器,但没有成功。我在搜索结果中有10篇文章,每页设置3条,这意味着结果列表将总共分为4页。但是,当我搜索关键字时,它在同一页面上显示了所有10篇文章,因此分页器无法正常工作。

我的 view.py 是:

def search_titles(request):
q = request.GET.get('q')
error_msg = ''

if not q:
    error_msg = 'Please enter keywords!'
    return render(request, 'search_results.html', {'error_msg': error_msg})
else:
    nq = q.split(' ')
    a_list = Q(article_ti__icontains=nq[0]) | Q(article_content__icontains=nq[0]) | Q(
        abstract__icontains=nq[0]) | Q(
        author__icontains=nq[0])
    for i in nq[1:]:
        a_list.add(Q(article_ti__icontains=i) | Q(article_content__icontains=i) | Q(abstract__icontains=i) | Q(author__icontains=i), a_list.connector)
    queryset = Article.objects.filter(a_list).distinct()
    queryset_count = queryset.count()

    paginator = Paginator(queryset, 3)
    page_var = 'page'
    page = request.GET.get(page_var, 1)
    try:
        sets = paginator.page(page)
    except PageNotAnInteger:
        sets = paginator.page(1)
    except EmptyPage:
        sets = paginator.page(paginator.num_pages)

return render(request, 'search_results.html', {'error_msg': error_msg, 'b_list': queryset, 'a_list_count': queryset_count, 'sets': sets, 'page_var':page_var})

我的 HTML 代码是:

<div class="ui text container">
    {% if error_msg %}
        <p>{{ error_msg }}</p>
    {% else %}
        {% for a in b_list %}
            <div class="ui segment">
                <a  target="_blank" href={{ a.get_abs_url }}>
                    <h3>{{ a.article_ti }}</h3>
                    <h5>{{ a.author }}</h5>
                    <p>{{ a.abstract }}</p>
                </a>
            </div>
    {% empty %}
        <div class="ui segment">No results for '{{ request.GET.q }}'</div>
    {% endfor %}
    {% endif %}
</div>

<div class="pagination" id="m">
<span class="step-links">
    {% if sets.has_previous %}
        <a href="?{{ page_var }}=1&q={{ request.GET.q }}" title="First"><<</a>
        <a href="?{{ page_var }}={{ sets.previous_page_number }}&q={{ request.GET.q }}" title="Previous"><</a>
    {% endif %}

    <span class="current"><input name="enter_page" value="{{ sets.number }}" id="page_num"> of {{ sets.paginator.num_pages }}</span>

    {% if sets.has_next %}
        <a href="?{{ page_var }}={{ sets.next_page_number }}&q={{ request.GET.q }}" title="Next">></a>
        <a href="?{{ page_var }}={{ sets.paginator.num_pages }}&q={{ request.GET.q }}" title="Last">>></a>
    {% endif %}
</span>
</div>

我在stackoverflow上尝试了几种方法,但仍然没有任何效果。有人可以给我一些建议吗?

如果您能帮助我,我将不胜感激!

1 个答案:

答案 0 :(得分:1)

在您的模板中,您正在遍历“ b_list”,

{% for a in b_list %}
        <div class="ui segment">
            <a  target="_blank" href={{ a.get_abs_url }}>
                <h3>{{ a.article_ti }}</h3>
                <h5>{{ a.author }}</h5>
                <p>{{ a.abstract }}</p>
            </a>
        </div>
{% empty %}
    <div class="ui segment">No results for '{{ request.GET.q }}'</div>
{% endfor %}

将此更改为

{% for a in sets %}
        <div class="ui segment">
            <a  target="_blank" href={{ a.get_abs_url }}>
                <h3>{{ a.article_ti }}</h3>
                <h5>{{ a.author }}</h5>
                <p>{{ a.abstract }}</p>
            </a>
        </div>
{% empty %}
    <div class="ui segment">No results for '{{ request.GET.q }}'</div>
{% endfor %}