feat: pagination

This commit is contained in:
eneller
2025-04-05 13:36:48 +02:00
parent 1ef440fb8f
commit 6e7d6b1e2a
3 changed files with 34 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, FileResponse, HttpResponseBadRequest from django.http import HttpRequest, HttpResponse, FileResponse, HttpResponseBadRequest
from django.core.paginator import Paginator
from epub2go.convert import get_all_books, Book, allbooks_url from epub2go.convert import get_all_books, Book, allbooks_url
@@ -34,16 +35,26 @@ def index(request: HttpRequest):
elif searchParam: elif searchParam:
localbooks = [book for book in books if searchParam in book.title] localbooks = [book for book in books if searchParam in book.title]
# return base view # paginate items
# TODO pagination paginationParam = request.GET.get('p', '')
try:
pageNo = int(paginationParam)
except ValueError:
logger.error('Failed to cast %s to int', paginationParam)
pageNo = 1
pages = Paginator(localbooks, 100)
if pageNo < 1 or pageNo > pages.num_pages:
pageNo = 1
page = pages.page(pageNo)
context = { context = {
'title': 'epub2go', 'title': 'epub2go',
'http_host': f'http://{ request.META['HTTP_HOST'] }', 'http_host': f'http://{ request.META['HTTP_HOST'] }',
'books': localbooks, 'page': page,
'books_count': len(localbooks),
'allbooks_count': len(books), 'allbooks_count': len(books),
'allbooks_url': allbooks_url, 'allbooks_url': allbooks_url,
} }
# return base view
return render(request, 'home.html', context) return render(request, 'home.html', context)
def validateUrl(param)->bool : def validateUrl(param)->bool :

View File

@@ -18,6 +18,8 @@ body{
font-size:18px; font-size:18px;
color:var(--fg); color:var(--fg);
padding:0 .2em; padding:0 .2em;
text-align: center;
justify-content: center;
} }
h1,h2,h3{ h1,h2,h3{
@@ -85,3 +87,6 @@ a:hover, a:any-link{
height: 100%; height: 100%;
padding: 1px; padding: 1px;
} }
.pagination a{
padding-left: 2em;
}

View File

@@ -16,7 +16,7 @@
<img src="{% static 'github.svg' %}" alt="GitHub" class="header-icon"> <img src="{% static 'github.svg' %}" alt="GitHub" class="header-icon">
</a> </a>
</div> </div>
<!-- NOTE use dl here?--> <small>{{ page.paginator.count }} Ergebnisse gefunden:</small>
<table id="table"> <table id="table">
<thead> <thead>
<tr> <tr>
@@ -26,7 +26,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for item in books %} {% for item in page.object_list %}
<tr class="table-entry"> <tr class="table-entry">
<td> <td>
<a href= {{ item.url }} target="_blank" rel="noopener noreferrer" class="table-link"> <a href= {{ item.url }} target="_blank" rel="noopener noreferrer" class="table-link">
@@ -45,6 +45,17 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<small>{{ books_count }} Ergebnisse gefunden</small> {% if page.has_other_pages %}
<div class="pagination">
{% if page.has_previous %}
<a href="?p=1"> &lt;&lt; </a>
<a href="?p={{ page.previous_page_number }}"> &lt; </a>
{% endif %}
{% if page.has_next %}
<a href="?p={{ page.next_page_number }}"> &gt; </a>
<a href="?p={{ page.paginator.num_pages }}"> &gt;&gt; </a>
{% endif %}
</div>
{% endif %}
</main> </main>
{% endblock %} {% endblock %}