Re: django-filter with pagination

3,315 views
Skip to first unread message

Steve

unread,
Dec 3, 2012, 12:25:16 PM12/3/12
to django...@googlegroups.com
The pagination only deals with the objects returned by the filtering. I also note that you aren't passing in the page of events to your template.

-Steve

On Mon, Dec 3, 2012 at 11:51 AM, Andy Warburton <andy.wa...@gmail.com> wrote:
I know this was asked back in 2010, but the link in the answer is no longer valid.

I wan't to paginate my django-filter results.  When I use Paginator in my view as follows, it breaks the filtering (I don't see any filter fields in my form):

def ca_index(request):
    latest_ca_list = eventFilter(request.GET, queryset=event.objects.all().order_by('-record_created'))  
    paginator = Paginator(latest_ca_list.qs, 100) # Show 100 events per page
    page = request.GET.get('page')
    try:
        events = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        events = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        events = paginator.page(paginator.num_pages)
    return render(request, 'xgenca/list.html', {'events': latest_ca_list})


Is anyone using django-filter with Paginator and if so how can this be made to work?

Andy Warburton

unread,
Dec 3, 2012, 2:25:59 PM12/3/12
to django...@googlegroups.com
Thanks for the reply.  So are you saying this should work?  

The last line of the code I posted should have been:

    return render(request, 'xgenca/list.html', {'events': events})

I'd changed it temporarily to get the filtering working.

Steve

unread,
Dec 3, 2012, 2:32:07 PM12/3/12
to django...@googlegroups.com
I would pass in your paginator object, since it also contains useful things such as how many there are, and such. also, you probably still want the django filter because it produces a form. the trick here is to get your filtering attributes into page links (next and previous).

-Steve

Andy Warburton

unread,
Apr 18, 2013, 2:27:56 PM4/18/13
to django...@googlegroups.com
Hi Tim,

Yes, the key was to use .qs to get the queryset from the filter:

       {% autopaginate events.qs 40 as filter_list %}  
       {% for event in filter_list %}
              <td>{{event.event_type.event_description}}
...

On Thursday, 18 April 2013 04:12:33 UTC+1, Tim Zenderman wrote:
Hey Andy, were you able to get it the Pagination to work?

Tim Zenderman

unread,
Apr 19, 2013, 7:46:43 PM4/19/13
to django...@googlegroups.com
Awesome, thanks!

Dmitry Karamin

unread,
Jan 13, 2017, 5:17:28 AM1/13/17
to django-filter
Hi all. i have a problem with django-filter and pagination. Probably someone can help me
when i use filter and click submit button, the url looks like this. 
   http://127.0.0.1:8000/catalog/yarns_filter/?color=grey&stockable=1
and pagination looks like this
Page 1 of 2. next
on this step everthing looks ok

but if i click next link, url looks like this 
http://127.0.0.1:8000/catalog/yarns_filter/?page=2

Now pagination ignores filter

code in template looks like this

<form action="" method="get">
        {{ filter.form.as_p }}
        <input type="submit" />
    </form>
    {% if events.object_list %}
        {% for product in events.object_list %}
            <div class="col-sm-6 col-md-3">
                <div class="thumbnail">
                    {% if product.image %}
                        <img src="{{ product.image.url }}" />
                    {% else %}
                        <img src="{% static 'main/img/noimage.jpg' %}" />
                    {% endif %}
                <div class="caption">
                    <h3>Номер по каталогу: {{ product.catalog }}</h3>
                    <p>Арт: {{ product.sku }}</p>
                    <p>Рал: {{ product.rall }}</p>
                    <p>Цвет: {{ product.color.template_name }}</p>
                    {% if product.type %}
                        {% if product.type == 'syntetic' %}
                            <p>Синтетическая</p>
                        {% else %}
                            <p>Хлопковая</p>
                        {% endif %}
                    {% endif %}
                    {% if product.stockable %}
                        <p><span class="label label-success">Складская позиция</span></p>
                    {% else %}
                        <p><span class="label label-danger">Под заказ</span></p>
                    {% endif %}
                    </div>
                </div>
            </div>
         {% endfor %}
    {% else %}
    <h3><span class="label label-default">Ничего  не найдено</span></h3>
    {% endif %}
 

and pagination 

<div class="pagination">
    <span class="step-links">
        {% if events.has_previous %}
            <a href="?page={{ events.previous_page_number }}">previous</a>
        {% endif %}
        <span class="current">
            Page {{ events.number }} of {{ events.paginator.num_pages }}.
        </span>
        {% if events.has_next %}
            <a href="?page={{ events.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>


so WTF? =) 




четверг, 18 апреля 2013 г., 21:27:56 UTC+3 пользователь Andy Warburton написал:

Carlton Gibson

unread,
Jan 13, 2017, 5:35:10 AM1/13/17
to django-filter
Hi Dmitry

Look at the URLs:

   http://127.0.0.1:8000/catalog/yarns_filter/?color=grey&stockable=1

vs

http://127.0.0.1:8000/catalog/yarns_filter/?page=2

You see the filter's query string parameters are not set. If you add them by hand the filter would work. Something like...


So, what you need to do is adjust the template where you generate the pagination link to account for the current query string. 

See this SO answer for an example of how: http://stackoverflow.com/a/22735278/102260

Dmitry Karamin

unread,
Jan 13, 2017, 7:31:01 AM1/13/17
to django-filter
Carlton, thank you very much for your reply
i didn't expect it so fast, so double thanks =))

Yes you are right. If a write page=2 by hands it will step to the second page with filtered objects.
I thought i do something wrong. I's so strange that this complicated library doesn't provide such a thing.

Thanks, again.
   

пятница, 13 января 2017 г., 13:35:10 UTC+3 пользователь Carlton Gibson написал:

Carlton Gibson

unread,
Jan 17, 2017, 5:35:13 AM1/17/17
to django-filter
Hi Dmitry,


Enjoy!

Regards,

Carlton
Reply all
Reply to author
Forward
0 new messages