Haystack Search Pager

To make haystack search pager place nicelly with other get parameters, we can do something like this: 1) Create a templatetag:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  1. @register.simple_tag
  2. def get_search_pager_link(request):
  3. params = []
  4. link = '?'
  5. for key, value in request.GET.items():
  6. if key != 'page':
  7. params.append(u'{}={}'.format(key, value))
  8. link += '&'.join(params)
  9. if len(link) > 1:
  10. link += '&'
  11. return link

2) Use it in your search pager template like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  1. {% load core_tags %}
  2. {% if page.has_previous or page.has_next %}
  3. <ul class="pagination">
  4. {% if page.has_previous %}
  5. <li class="previous"><a
  6. href="{% get_search_pager_link request %}page={{ page.previous_page_number }}">previous</a>
  7. </li>
  8. {% endif %}
  9. {% for pg in page.paginator.page_range %}
  10. {% if page.number == pg %}
  11. <li class="active"><a
  12. href="{% get_search_pager_link request %}page={{ pg }}">{{ pg }}</a></li>
  13. {% else %}
  14. <li><a href="{% get_search_pager_link request %}page={{ pg }}">{{ pg }}</a></li>
  15. {% endif %}
  16. {% endfor %}
  17. {% if page.has_next %}
  18. <li class="next"><a
  19. href="{% get_search_pager_link request %}page={{ page.next_page_number }}">next</a>
  20. </li>
  21. {% endif %}
  22. </ul>
  23. {% endif %}

That way we'll achieve some nice functionality: search pager will play well with other GET parameters.