Added page navigation buttons to the gallery view.

This commit is contained in:
2023-08-19 23:37:19 -04:00
parent eeb88057e7
commit 43b74c1521
3 changed files with 50 additions and 23 deletions

BIN
viewer/static/imgs/forward.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -20,6 +20,16 @@
<div class="mb-2"> <div class="mb-2">
{% for page in pages %}<a href="./?page={{page}}">{{page}}</a>{% if not forloop.last %}<span> </span>{% endif %}{% endfor %} {% for page in pages %}<a href="./?page={{page}}">{{page}}</a>{% if not forloop.last %}<span> </span>{% endif %}{% endfor %}
</div> </div>
<table class="fc mauto">
<tr>
{% if page != 1 %}
<td class="fc">
<a href="./?page={{prev_page}}">
<img src="{% static 'imgs/back.png' %}" class="navigation-icon">
</a>
</td>
{% endif %}
<td class="fc">
<table class="mauto"> <table class="mauto">
<tbody> <tbody>
{% for row in images %} {% for row in images %}
@@ -39,6 +49,16 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
</td>
{% if page != num_pages %}
<td class="fc">
<a href="./?page={{next_page}}">
<img src="{% static 'imgs/forward.png' %}" class="navigation-icon">
</a>
</td>
{% endif %}
</tr>
</table>
</div> </div>
{% endif %} {% endif %}
{% if subdirs|length > 0 %} {% if subdirs|length > 0 %}

View File

@@ -53,13 +53,14 @@ def gallery_view(request, path = None):
subdirs = sorted([i for i in full_path.iterdir() if i.is_dir()]) subdirs = sorted([i for i in full_path.iterdir() if i.is_dir()])
images = sorted([i for i in full_path.iterdir() if i.is_file() and filetype.is_image(str(i))]) images = sorted([i for i in full_path.iterdir() if i.is_file() and filetype.is_image(str(i))])
img_data = [] img_data = []
num_pages = ceil((len(images) / CELLS_PER_ROW) / ROWS_PER_PAGE)
try: try:
page = int(request.GET.get('page', 1)) - 1 page = int(request.GET.get('page', 1))
except ValueError: except ValueError:
page = 0 page = 1
page_base = IMAGES_PER_PAGE * page page_base = IMAGES_PER_PAGE * (page - 1)
for image in images[page_base : page_base + IMAGES_PER_PAGE]: for image in images[page_base : page_base + IMAGES_PER_PAGE]:
make_thumbnail(image) make_thumbnail(image)
@@ -72,12 +73,18 @@ def gallery_view(request, path = None):
img_data.append(img_context) img_data.append(img_context)
print(page)
context = { context = {
'path': path, 'path': path,
'subdirs': list2rows(subdirs), 'subdirs': list2rows(subdirs),
'images': list2rows(img_data), 'images': list2rows(img_data),
'pages': range(1, ceil((len(images) / CELLS_PER_ROW) / ROWS_PER_PAGE) + 1), 'pages': range(1, num_pages + 1),
'num_files': len(images) 'num_files': len(images),
'page': page,
'prev_page': page - 1,
'next_page': page + 1,
'num_pages': num_pages
} }
return render(request, 'gallery_view.html', context) return render(request, 'gallery_view.html', context)