1

我有一个包含多个字段的搜索表单,并开发了一个允许搜索任何字段组合的搜索视图。这可以按预期工作并提供结果,直到第一次导航(使用“下一步”按钮)到分页创建对象的下一页。导航到“下一个”后,“ct”字段变为“无”。这是视图:

def search(request):
    q = request.GET.get('q')
    yr = request.GET.get('yr', 0)
    ct = request.GET.get('ct')
    question_set = Uroquiz.objects.all()

    if q:
        if not (yr or ct):
            question_set = Uroquiz.objects.filter(question__icontains=q)

    if yr:
        if not (q or ct):
            question_set = Uroquiz.objects.filter(year=yr)

    if ct:
        if not (q or yr):
            question_set = Uroquiz.objects.filter(cat__icontains=ct)

    if (q and yr):
        if not ct:
            question_set = Uroquiz.objects.filter(question__icontains=q).filter(year=yr)

    if (q and ct):
        if not yr:
            question_set = Uroquiz.objects.filter(question__icontains=q).filter(cat__icontains=ct)
    if (yr and ct):
        if not q:
            question_set = Uroquiz.objects.filter(year=yr).filter(cat__icontains=ct)
    if ((q and yr) and ct):
        question_set = Uroquiz.objects.filter(question__icontains=q).filter(year=yr).filter(cat__icontains=ct)
    if not ((q or yr) or ct):
            return render_to_response('search_form.html')
    paginator = Paginator(question_set, 1)
    page = request.GET.get('page')
    try:
        question_set = paginator.page(page)
    except PageNotAnInteger:
        question_set = paginator.page(1)
    except EmptyPage:
        question_set = paginator.page(paginator.num_pages)
    return render_to_response('search_results.html',
        {'question': question_set, 'q': q, 'yr': yr, 'ct': ct})

这是我的 search_results.html 的相关部分:

<div class="pagination">
    <span class="step-links">
        {% if question.has_previous %}
            <a href="?q={{ q }}&yr={{ yr }}&cat={{ ct }}&page={{ question.previous_page_number }}">previous</a>
        {% endif %}

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

        {% if question.has_next %}
            <a href="?q={{ q }}&yr={{ yr }}&cat={{ ct }}&page={{ question.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>
4

1 回答 1

0

我想到了。我的网址应该使用“ct”而不是“cat”。简单的错误。感谢所有为我看这个的人。

于 2013-02-04T05:41:07.643 回答