【问题标题】:Django: each paginated page shows the same resultsDjango:每个分页页面显示相同的结果
【发布时间】:2012-09-24 20:03:01
【问题描述】:

所以我尝试使用分页来显示某一天可用的所有匹配类,但是按照分页文档,每个页面只返回相同的 10 个结果。我错过了什么/我应该在 urlconf 中有什么?此外,如果我尝试使用分页来显示搜索结果,当我尝试选择下一页时,我会收到错误“视图 search.views.search_classes 没有返回 HttpResponse 对象”。将不胜感激对任何一个或两个示例的任何输入。

#views.py
def upcoming_class_list(request, day):
    try:
        day = int(day)
    except ValueError:
        raise Http404()
    today = datetime.date.today()
    day_x = datetime.date.today() + datetime.timedelta(days=day)
    day_x_classes = UpcomingClasses.objects.filter(class_date=day_x)
    all_matches = day_x_classes
    paginator = Paginator(all_matches, 10)
    page = request.GET.get('page')
    try:
        matches = paginator.page(page)
    except PageNotAnInteger:
        matches = paginator.page(1)
    except EmptyPage:
        matches = paginator.page(paginator.num_pages)
    return render_to_response('preview.html', {'today': today, 'tomorrow': tomorrow,
            'past_classes': past_classes, 'day_x': day_x, 'day': day,
            'day_x_classes': day_x_classes, 'yesterday': yesterday, 'matches': matches,
            'page': page}, context_instance = RequestContext(request))

#urls.py
(r'^upcoming_class_list/plus/(\d{1,2})/$', upcoming_class_list),

#preview.html
<h3>Classes for {{ day_x }}</h3>
{% if matches %}
    <div class="pagination">
        <span class="step-links">
            {% if matches.has_previous %}
            <a href="?page={{ matches.previous_page_number }}">&laquo; previous </a>
            {% endif %}

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

            {% if matches.has_next %}
            <a href="?page={{ matches.next_page_number }}"> next &raquo;</a>
            {% endif %}
        </span>
    </div>

    {% if day_x_classes %}
        <ul type=none>
        {% for class in day_x_classes %}
            <li>
                <ul type=none>
                    <li><strong>{{ class.type }}</strong></li>
                    <li>Teacher: <a href="/profiles/{{ class.teacher }}">{{ class.teacher }}</a></li>
                    <li>Class Description: {{ class.description }}</li>
                    ...
                </ul>
            </li><br />
        {% endfor %}
        </ul>
    {% endif %}
{% else %}
<p>There are currently no scheduled upcoming classes for {{ day_x }}.</p>
{% endif %}

【问题讨论】:

    标签: django pagination django-urls


    【解决方案1】:

    来自 GET 或 POST 的任何内容都是字符串,因此您总是遇到第一个异常。试试:

    try:
        matches = paginator.page(int(page))
    except (PageNotAnInteger, ValueError):
        matches = paginator.page(1)
    

    如果不查看您的其他观点,很难猜测问题的其余部分。查看视图中的其他位,您不需要检查 day 是否为 int,因为您已经确保在您的 urls.py 文件中使用正则表达式,但您 don't call the Http404 object,它只是 raise Http404

    【讨论】:

    • 我的其余部分只是...... return render_to_response('preview.html', {'today': today, 'tomorrow': tomorrow, 'past_classes': past_classes, 'day_x': day_x,'day':day,'day_x_classes':day_x_classes,'yesterday':昨天,'matches':匹配,'page':page},context_instance = RequestContext(request)) ...如果我尝试matches = paginator。 page(int(page)) 我得到 TypeError: int() 参数必须是字符串或数字,而不是 'NoneType'
    • paginator.page('1') 工作正常。 Django 在内部强制转换为 int。
    • 此外,我已经确定我的所有匹配结果都显示在每个页面上。例如,我有 paginator=Paginator(all_matches, 10),但是,所有 11 个结果都将显示在第一页上。我正在使用 Django 1.4,我基本上完全按照文档进行操作,至少据我所知,除非我遗漏了什么。
    【解决方案2】:

    好的,所以我找到了两个问题的答案。第一部分是因为我在模板中犯了一个愚蠢的错误。视图 urlconf 是正确的,但在我的模板中,我的 for 循环声明:

    {% for class in day_x_classes %}
    

    我应该在什么时候使用

    {% for class in matches %}
    

    因为比赛是分页的,而不是 day_x_classes。 至于对我的搜索结果进行分页,我只需要从

    编辑“上一个”和“下一个”按钮
    <a href="?q={{ query }}&page={{ matches.previous_page_number }}">&laquo; previous </a>
    

    来自

    <a href="?page={{ matches.previous_page_number }}">&laquo; previous</a>
    

    考虑 q(搜索词)。

    我希望我的错误能够帮助陷入类似情况的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      相关资源
      最近更新 更多