同一搜索页面中的Django分页

时间:2018-07-25 17:18:14

标签: django pagination django-queryset

我在与搜索相同的页面上显示搜索结果。我正在尝试使用django中的分页对我的结果进行分页。分页有效,除非我单击下一个或上一个,否则页面会重新加载。我知道我必须在页面中传递一些要在模板中添加的内容,但是我有点迷失了。 这是我的观点:

def lookup(request):
    if request.method == "POST":
        form = SearchForm(request.POST)
        now = datetime.datetime.now()
        if form.is_valid():
            l_city = request.POST['city']
            l_state = request.POST['state']
            l_country = request.POST['country']
            result_list = Listing.objects.filter(
            location_Country__icontains=l_country).filter(
            location_City__icontains=l_city).filter(
            location_State__icontains=l_state).order_by('listing_from_date')

            paginator = Paginator(result_list, 5)  # Show 5 contacts per page

            page = request.GET.get('page')
            try:
                result = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                result = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                result = paginator.page(paginator.num_pages)

            return render (request, 'lookup.html', {'result':result})

    empty_data = False
    return render (request, 'lookup.html', {'empty_data':empty_data})

这是我的模板代码:

{% for results in result %} display my list {%endfor%}
<div class="pagination">
    <span class="step-links">
        {% if result.has_previous %}
            <a href="?page={{ result.previous_page_number }}">previous</a>
        {% endif %}

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

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

我认为这与POST和GET的处理有关,尽管我会沿{{result.net_page_number}}传递一些东西,但是我可以传递什么呢? 谢谢。

1 个答案:

答案 0 :(得分:1)

def lookup(request):
    listing = Listing.objects.all()

    if not request.method == 'POST':
        if 'search-persons-post' in request.session:
            request.POST = request.session['search-persons-post']
            request.method = 'POST'

    if request.method == "POST":
        request.session['search-persons-post'] = request.POST
        form = SearchForm(request.POST)
        now = datetime.datetime.now()
        if form.is_valid():
            l_city = request.POST['city']
            l_state = request.POST['state']
            l_country = request.POST['country']
            result_list = Listing.objects.filter(country__icontains=l_country).filter(
                city__icontains=l_city).filter(state__icontains=l_state).order_by('listing_from_date')

            paginator = Paginator(result_list, 5)  # Show 5 contacts per page

            page = request.GET.get('page')
            try:
                result = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                result = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                result = paginator.page(paginator.num_pages)

            return render(request, 'lookup.html', {'result': result})

    empty_data = False
    return render(request, 'lookup.html', {'empty_data': empty_data})