11from django .contrib .auth .decorators import login_required
2+ from django .core .paginator import Paginator
23from django .template .response import TemplateResponse
34
45from djsfc import Router , parse_template
910
1011router = Router (__name__ )
1112
13+ WARRIORS_PER_PAGE = 25
14+
1215
1316template = parse_template ('''\
1417 {% extends "base.html" %}
2528 </tr>
2629 </thead>
2730 <tbody>
28- {% for warrior in warriors %}
31+ {% for warrior in page_obj %}
2932 <tr>
3033 <td>{{ warrior.name }}</td>
3134 {% for arena, warrior_arena in warrior.warrior_arena_objects %}
4144 {% endfor %}
4245 </tbody>
4346 </table>
47+
48+ {% if page_obj.has_other_pages %}
49+ <nav class="pagination">
50+ {% if page_obj.has_previous %}
51+ <a href="?page=1">« first</a>
52+ <a href="?page={{ page_obj.previous_page_number }}">previous</a>
53+ {% endif %}
54+
55+ <span class="current">
56+ Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
57+ </span>
58+
59+ {% if page_obj.has_next %}
60+ <a href="?page={{ page_obj.next_page_number }}">next</a>
61+ <a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
62+ {% endif %}
63+ </nav>
64+ {% endif %}
4465{% endblock %}
4566''' , router = router )
4667
@@ -51,14 +72,20 @@ def index(request):
5172 listed_arenas = list (Arena .objects .filter (
5273 listed = True ,
5374 ))
54- warriors = list ( Warrior .objects .filter (
75+ warriors = Warrior .objects .filter (
5576 users = request .user ,
56- ).prefetch_related ('warrior_arenas' ).order_by ('name' ))
57- for warrior in warriors :
77+ ).prefetch_related ('warrior_arenas' ).order_by ('name' )
78+
79+ paginator = Paginator (warriors , WARRIORS_PER_PAGE )
80+ page_number = request .GET .get ('page' )
81+ page_obj = paginator .get_page (page_number )
82+
83+ for warrior in page_obj :
5884 warrior .warrior_arena_objects = get_warrior_arena_objects (warrior , listed_arenas )
85+
5986 context = {
6087 'listed_arenas' : listed_arenas ,
61- 'warriors ' : warriors ,
88+ 'page_obj ' : page_obj ,
6289 }
6390 return TemplateResponse (request , template , context )
6491
0 commit comments