Django-filter: Document how django-filter works with django-pagination

Created on 13 Aug 2009  路  20Comments  路  Source: carltongibson/django-filter

They both work well together, it took me a while to fiddle it out. Based on the example in the docs:

{% block content %}
    <form action="" method="get">
        {{ f.form.as_p }}
        <input type="submit" />
    </form>

    {% autopaginate f.qs 40 as filter_list %}

    {% for obj in filter_list %}
        {{ obj.name }} - ${{ obj.price }}<br />
    {% endfor %}

    {% paginate %}
{% endblock %}

The key is that you have to use pagination's _as_ argument.

Most helpful comment

Very, very useful. Thank you.

All 20 comments

Very, very useful. Thank you.

I'd suggest to document how this works with django-sorting too. Unfortunately django-sorting needs to go before pagination and doesn't support the 'as' keyword, so we are _not_ able to do something like this:

{% autosort f.qs as actionlog %}
{% autopaginate actionlog 30 %}

Filed upstream at http://github.com/directeur/django-sorting/issues/#issue/4.

Get this issue when I try the above.

http://code.google.com/p/django-pagination/issues/detail?id=59#c0

Any ideas about what it might be?

There is a problem with django-pagination: when you are, say, at page 5 and you apply a filter, the filter pass the "page" GET variable too, so in the filtered page you are already at page 5, that is wrong. Is there a way to exclude from the url the variable that django-pagination use when apply a filter? Hope this make sense...

mdgart,

I solved the page variable issue on the client with jQuery and the jQuery Back Button & Query (BBQ) library. See: http://benalman.com/code/projects/jquery-bbq/docs/files/jquery-ba-bbq-js.html

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}js/jquery.ba-bbq.min.js"></script>
<script type="text/javascript">
  // on page load complete
  $(function(){
    // force all filter links to return to page 1
    $('#changelist-filter a').querystring('page=1');
  });
</script>

When the user clicks a filter link, javascript forces the get parameter of "page" to 1.

In my example, all my filters are displayed as links, located within a div with an id of "changelist-filter". You will need to change your jQuery selector as appropriate.

Hope this helps.

mdgart, jonathonadler,

I solved the page variable problem in a slightly different way that avoids relying on javascript: simply don't pass the 'page' parameter to the FilterSet. For example, based on the example in the docs:
def product_list(request):
data = request.GET.copy()
if 'page' in data:
del data['page']
f = ProductFilter(data, queryset=Product.objects.all())
return render_to_response('my_app/template.html', {'filter': f})
Hope to help.

Very neat solution richardbarran, thanks!

Thanks. I thought of patching the documentation to talk about django-pagination, but I have a small problem: what format is the documentation in? :-(
It's not rst, as docutils complains about it (at least on my computer). Any suggestions?

Richard, it is rst but needs to be processed using sphinx (http://sphinx.pocoo.org/).

The moment I use autopaginate tag, I get "object of type 'fooFilter' has no len()" typeerror. I'm sure I've got the enurable result, because I can loop though it and display the objects without using django-pagination. How can I fix that? Thanks!

Excellent, saved me a lot of time thanks.

Could you give me github links to django-pagination and django-sorting which work together, I tried some of these repositories without success. I get a :

TemplateSyntaxError at /ads/search/
Caught VariableDoesNotExist while rendering: Failed lookup for key [sorted_objects] in u'[{}, {\'csrf_token\' ...

using this template

    {% autosort filter.qs as sorted_objects %}
    {% autopaginate sorted_objects 10 as object_list %}

    {% for object in object_list %}
        {{ object }}
    {% endfor %}

    {% paginate %}  

    <ul>
        <li>{% anchor price "First Field" %}</li>
        <li>{% anchor surface "Other Field" %}</li>
    </ul>

Thanks !

Sorry, I haven't used django-sorting so can't really help.

Good luck!

NB

Could you give me github links to django-pagination and django-sorting which work together, I tried some of these repositories without success. I get a :

TemplateSyntaxError at /ads/search/
Caught VariableDoesNotExist while rendering: Failed lookup for key [sorted_objects] in u'[{}, {\'csrf_token\' ...

using this template

  {% autosort filter.qs as sorted_objects %}
  {% autopaginate sorted_objects 10 as object_list %}

  {% for object in object_list %}
      {{ object }}
  {% endfor %}

  {% paginate %}  

  <ul>
      <li>{% anchor price "First Field" %}</li>
      <li>{% anchor surface "Other Field" %}</li>
  </ul>

Thanks !

so, here is a working pip requirements example for my previous question:

django-filter==0.5.3
django-pagination==1.0.7
-e git://github.com/lukeman/django-sorting.git#egg=django_sorting

Does anyone know if using pagination and sorting with django-filter is possible using generic views? I see from these examples that the generic view method isn't being used.

If I understand your question, it's possible, I use it here: https://github.com/ouhouhsami/django-geoads/blob/master/geoads/views.py

In case anyone is interested, I wrote a much more complete replacement for django-sorting, called django-sorter: http://django-sorter.readthedocs.org/

As part of getting on top of the issue tracker, I'm going to close this one. It's more than 2 years since the last comment.

If anybody wants to send in a pull request updating the documentation showing integration with current pagination/sorting solutions I'd be really happy to review, but short of that I'll come back to this next time it comes up _IRL_.

I hope that makes sense.

Thanks!!!

Was this page helpful?
0 / 5 - 0 ratings