分页在django的每个页面上显示相同的结果

时间:2014-08-22 16:37:30

标签: django pagination django-templates django-pagination

我有以下视图来获取和分页搜索结果:

def search(request):

    query=request.GET.get('q','')

    form=SearchForm({'q': query })

    results = form.search()

    paginator = Paginator(results, 15)

    print 'page is: '+request.GET.get('page')

    try:
        page = paginator.page(int(request.GET.get('page')))
    except InvalidPage:
        raise Http404("No such page of results!")

    for result in page.object_list:
        print result.object.name
    #this prints the same every time!




    context = {
        'view':resolve(request.path_info).url_name,
        'form':form,
        'page':page,
        'query':query,
    }

    return render(request, 'nutrition/food-log.html', context)

我的模板如下所示:

{% if query %}
            <h3>Results</h3>

            {% for result in page.object_list %}
                <p>{{ result.object.name }}</p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}

            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}

有谁知道为什么会这样?

0 个答案:

没有答案