0
votes

I am displaying the results of a search in the same page as the search. I am tryin gto use the pagination in django to, well, paginate my results. The pagination works, except when I click on next or previous, the page reloads to scratch. I know I have to pass something in the template to add after the page, but I am kind of lost. Here is my view:

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})

and here is my template code:

{% 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>

I assume this has to do with the handling of the POST and GET, I though about passing something along the {{ result.net_page_number}}, but what can I pass? Thank you.

1

1 Answers

1
votes
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})