Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how django-filter works with django-pagination #13

Closed
bartTC opened this issue Aug 13, 2009 · 20 comments
Closed

Document how django-filter works with django-pagination #13

bartTC opened this issue Aug 13, 2009 · 20 comments

Comments

@bartTC
Copy link

bartTC commented Aug 13, 2009

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.

@glezos
Copy link

glezos commented Aug 14, 2009

Very, very useful. Thank you.

@glezos
Copy link

glezos commented Aug 18, 2009

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.

@chokosabe
Copy link

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?

@mdgart
Copy link

mdgart commented Feb 2, 2010

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...

@jonathonadler
Copy link

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.

@richardbarran
Copy link

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.

@mdgart
Copy link

mdgart commented Mar 23, 2010

Very neat solution richardbarran, thanks!

@richardbarran
Copy link

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?

@vangale
Copy link

vangale commented May 28, 2010

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

@amiroff
Copy link

amiroff commented Jun 21, 2010

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!

@wedgeski
Copy link

Excellent, saved me a lot of time thanks.

@ouhouhsami
Copy link

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 !

@wedgeski
Copy link

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 !

@ouhouhsami
Copy link

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

@camilonova
Copy link

Try to use my fork:
https://github.com/camilonova/django-sorting

@kmmathis
Copy link

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.

@ouhouhsami
Copy link

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

@jezdez
Copy link
Contributor

jezdez commented May 10, 2012

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

@carltongibson
Copy link
Owner

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.

@montenegroariel
Copy link

Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests