Jordan Raychev
Jordan Raychev

Reputation: 41

Django pagination resets

I am building an application that lists certain records from a database. The database is quite large and I am building a pagination to show a specific number of rows. Currently I have set a default value for the paginate_by. Here is the class that I am using.

class HomePageView(ListView):
    model = Syslog
    template_name = 'home.html'
    context_object_name = 'all_logs';
    paginate_by = 10

    def get_paginate_by(self, queryset):
        return self.request.GET.get("paginate_by", self.paginate_by)

I have implemented and a feature that allows clients to change the pagination levels like so:

<form action="" method="get">
    <select name="paginate_by" onchange='this.form.submit()'>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
    </select>
</form>

When the home page is loaded with the default value the number of pages show correctly. When a user submit another paginate_by value the number of pages change accordingly. The problem is that when the pagination parameter is changed and a user click on let's say 2nd page the paginate_by resets to the default.

Regards, Jordan

Upvotes: 1

Views: 553

Answers (2)

Jordan Raychev
Jordan Raychev

Reputation: 41

I have figure out a way to do what I was looking for, so I ended up with this.

This is how the form looks like.

<form action="" method="get" id="form">
 <select name="paginate_by" onchange = "this.form.submit();">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>
</form>

For the pagination feature I ended up using a custom template tag that alters the ?page parameter, so when a client clicks on ?page=1 for example, he is redirected to www.example.com/?paginate_by=2&page=1 and if he clicks on ?page=2, he is redirected to www.example.com/?paginate_by=2&page=2 (without the custom tag he would be redirected to www.example.com/?paginate_by=2&page=1&page=2)

Upvotes: 0

Kishan Parmar
Kishan Parmar

Reputation: 820

you have to first get request path and after that you have to put a page number in

    {{ request.path }}  #  -without GET parameters 

{{ request.get_full_path }}  # - with GET parameters

you have to use without get parameters

 <select name="report" onchange="location = this.value;" class="form-control">
    <option value="{{request.path}}?page=page_logic">1</option>
    </select>

here page_logic can be 1,2,3,4 ...

Upvotes: 1

Related Questions