1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +00:00

Fixed bug with paging where the paginator thought the current page was the next page.

Also changed the sort button URLs so when changing sort, you are thrown back to page 1.
This commit is contained in:
Samuel Lai 2012-02-04 18:38:15 +11:00
parent 5078e7369f
commit 3e02dcf151
2 changed files with 11 additions and 9 deletions

View File

@ -222,9 +222,9 @@ def search():
if not query:
redirect(settings.APP_URL_ROOT)
# the page GET parameter is zero-based
page = int(request.GET.get('p', 0))
# page needs to be zero-based for pysolr
page = (page > 0) and (page - 1) or page
if page < 0: page = 0
rows_per_page = int(request.GET.get('r', 10))
rows_per_page = (rows_per_page > 0) and rows_per_page or 10
@ -256,7 +256,7 @@ def search():
context['query'] = query
context['results'] = results
context['total_hits'] = results.hits
context['current_page'] = page + 1 # page should be ones-based
context['current_page'] = page + 1 # page template var is ones-based
context['rows_per_page'] = rows_per_page
context['total_pages'] = int(math.ceil(float(results.hits) / rows_per_page))
context['sort_by'] = sort_by

View File

@ -12,9 +12,9 @@
</form>
<ul class="tabs">
<li {% if sort_by == 'newest' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=newest') }}">newest</a></li>
<li {% if sort_by == 'votes' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=votes') }}">votes</a></li>
<li {% if sort_by == 'relevance' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=relevance') }}">relevance</a></li>
<li {% if sort_by == 'newest' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=newest', 'p=0') }}">newest</a></li>
<li {% if sort_by == 'votes' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=votes', 'p=0') }}">votes</a></li>
<li {% if sort_by == 'relevance' %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('s=relevance', 'p=0') }}">relevance</a></li>
</ul>
</div>
</div>
@ -58,15 +58,17 @@
<div class="pagination">
<ul>
{% if current_page > 1 %}
<li class="prev"><a href="{{ REQUEST.url|set_get_parameters('p=' ~ (current_page - 1)) }}">&larr; Previous</a></li>
{# the prev page is current_page - 2 because current_page is ones-based, but the p GET parameter is zero-based #}
<li class="prev"><a href="{{ REQUEST.url|set_get_parameters('p=' ~ (current_page - 2)) }}">&larr; Previous</a></li>
{% else %}
<li class="prev disabled"><a href="#">&larr; Previous</a></li>
{% endif %}
{% for p in range(1, total_pages + 1) %}
<li {% if p == current_page %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('p=' ~ p) }}">{{ p }}</a></li>
<li {% if p == current_page %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('p=' ~ (p-1)) }}">{{ p }}</a></li>
{% endfor %}
{% if current_page != total_pages %}
<li class="next"><a href="{{ REQUEST.url|set_get_parameters('p=' ~ (current_page + 1)) }}">&rarr; Next</a></li>
{# the next page is just current_page because current_page is ones-based, but the p GET parameter is zero-based #}
<li class="next"><a href="{{ REQUEST.url|set_get_parameters('p=' ~ current_page) }}">&rarr; Next</a></li>
{% else %}
<li class="next disabled"><a href="#">&rarr; Next</a></li>
{% endif %}