5

我有一个搜索视图,允许用户搜索他们的记事卡,然后一次显示分页 6 的结果。

我在所有其他视图中都使用了分页,它可以正常工作。但是,将它与查询一起使用时,它会显示前 6 个结果,显示“第 1 页,共 2 页”和下一个按钮,但是当您单击“下一步”时,您找不到任何结果。

我已将“*”查询设置为全部搜索,当我搜索该查询时,第一个请求调用是:

http://127.0.0.1:8000/search/?q=*

当我单击下一步时,调用是:

http://127.0.0.1:8000/search/?page=2

搜索网址是:

url(r'^search/', 'notecard.search.views.search', name="search"),

搜索视图是:

@login_required(login_url='/auth/login/')
def search(request):
    query = request.GET.get('q', '')
    results = []
    notecard_list = []
    if query:
        if query == "*":
            results = Notecard.objects.filter(section__semester__user=request.user)
        else:
            results = Notecard.objects.filter(Q(section__semester__user=request.user), Q(notecard_body__icontains=query)|Q(notecard_name__icontains=query))
        paginator = Paginator(results, 6)

        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1

        try:
            notecard_list = paginator.page(page)
        except (EmptyPage, InvalidPage):
            notecard_list = paginator.page(paginator.num_pages)
    return list_detail.object_list(
        request,
        queryset = Notecard.objects.filter(section__semester__user=request.user),
        template_name = "notecards/search.html",
        template_object_name = "results",
        extra_context = {"results": results, "notecard_list": notecard_list,},
    )

搜索模板是:

{% extends "base.html" %}
{% block title %} Notecard List | {% endblock %}

{% block content %}
<div id='main'>
<table id='notecards'>
  <tr>
        <th class="name">Search Results</th>
  </tr>
</table>

<table id='known_split'>
        <p>Search results:<a class="edit" href="{% url semester_list %}">Back to Semesters</a></p>
            {% for result in notecard_list.object_list %}
                    <tr>
                        <td class="image">{% if result.known %}<img src="{{ STATIC_URL }}/images/known.png" width="41" height="40" />{% else %}<img src="{{ STATIC_URL }}/images/unknown.png" width="41" height="40" />{% endif %}</td>
                    <td class="text"><a href="{% url notecards.views.notecard_detail result.id %}">{{ result.notecard_name|truncatewords:9 }}</a></td>
                    </tr>
                {% endfor %}
</table>

<div class="pagination">
  <span class="step-links">
{% if notecard_list.has_previous %}
<a class="navlink" href="?page={{ notecard_list.previous_page_number }}">previous</a>
{% endif %}

<span class="current">
Page {{ notecard_list.number }} of {{ notecard_list.paginator.num_pages }}
</span>

{% if notecard_list.has_next %}
<a class="navlink" href="?page={{ notecard_list.next_page_number }}">next</a>
{% endif %}
  </span>
</div>

{% endblock %}

正如我所说,我在其他所有视图中都成功地使用了基本相同的分页,所以我必须相信这是由于查询造成的。

4

2 回答 2

4
  1. 您没有q在第 2 页的 URL 中提供参数(URL 应该类似于http://127.0.0.1:8000/search/?q=*&page=2
  2. 您应该查看基于类的视图(ListView更准确地说),其中包括分页
于 2012-05-01T18:16:15.293 回答
1

需要在URL中包含q参数,has_previous & has_next

<a class="navlink" href="?page={{ notecard_list.previous_page_number }}&q1={{ request.GET.q1 }}">previous</a>

和,

<a class="navlink" href="?page={{ notecard_list.next_page_number }}&q1={{ request.GET.q1 }}">next</a>
于 2017-06-04T16:11:55.007 回答