Browse files

Added pagination to all_games.html

  • Loading branch information...
1 parent 4b83ec3 commit b584dee475dd9b1596067add5d7a8c4179aa8980 @arnlaugsson arnlaugsson committed Mar 15, 2012
Showing with 38 additions and 3 deletions.
  1. +13 −2 core/views.py
  2. +25 −1 templates/core/all_games.html
View
15 core/views.py
@@ -2,7 +2,7 @@
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
-
+from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import json
@@ -50,7 +50,18 @@ def all_teams(request):
context_instance=RequestContext(request))
def all_games(request):
- games = Game.objects.all().order_by('-date')
+ game_list = Game.objects.all().order_by('-date')
+ paginator = Paginator(game_list, 15) # Show 15 games per page
+
+ page = request.GET.get('page', 1)
+
+ try:
+ games = paginator.page(page)
+ except PageNotAnInteger:
+ games = paginator.page(1)
+ except EmptyPage:
+ games = paginator.page(paginator.num_pages)
+
ctx = {'games': games}
return render_to_response('core/all_games.html', ctx,
context_instance=RequestContext(request))
View
26 templates/core/all_games.html
@@ -10,7 +10,7 @@
</tr>
</thead>
<tbody>
- {% for game in games %}
+ {% for game in games.object_list %}
<tr>
<td>{{ game.date|date }}</td>
<td><a href="{% url view-team game.home_team.pk %}">{{ game.home_team }}</a></td>
@@ -20,4 +20,28 @@
{% endfor %}
</tbody>
</table>
+<div class="pagination">
+ <ul>
+ {% if games.has_previous %}
+ <li class="prev"><a href="?page={{ games.previous_page_number }}">← fyrri</a></li>
+ {% else %}
+ <li class="prev disabled"><a href="#">← fyrri</a></li>
+ {% endif %}
+
+ {% for i in games.paginator.page_range %}
+ {% if forloop.counter == games.number %}
+ <li class="active"><a href="?page={{ forloop.counter }}">{{ forloop.counter }}</a></li>
+ {% else %}
+ <li><a href="?page={{ forloop.counter }}">{{ forloop.counter }}</a></li>
+ {% endif %}
+ {% endfor %}
+
+ {% if games.has_next %}
+ <li class="next"><a href="?page={{ games.next_page_number }}">næsta →</a></li>
+ {% else %}
+ <li class="next disabled"><a href="#">næsta →</a></li>
+ {% endif %}
+
+ </ul>
+</div>
{% endblock main %}

0 comments on commit b584dee

Please sign in to comment.