From 039778e9a90b234ecb0c98dc06b77ebee17d92e2 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Fri, 12 Mar 2021 21:16:23 +0100 Subject: [PATCH] Add paginator --- apps/back/consumers.py | 7 ++- apps/front/templates/layouts/main.html | 32 ++----------- .../templates/partials/blog/all_articles.html | 42 +++++++++++++++--- apps/front/views.py | 3 +- db.sqlite3 | Bin 757760 -> 757760 bytes 5 files changed, 49 insertions(+), 35 deletions(-) diff --git a/apps/back/consumers.py b/apps/back/consumers.py index b51afdd..7e5366b 100644 --- a/apps/back/consumers.py +++ b/apps/back/consumers.py @@ -31,7 +31,12 @@ class BlogConsumer(WebsocketConsumer): # Database if template == "partials/blog/all_articles.html": - data["posts"] = Post.objects.all()[:5] + pag = data['pag'] if 'pag' in data else 1 + amount = 3 + start = pag - 1 + end = start + amount + data["posts"] = Post.objects.all()[start:end] + data['pag'] = pag if template == "partials/blog/single.html": data["post"] = Post.objects.get(data['id']) diff --git a/apps/front/templates/layouts/main.html b/apps/front/templates/layouts/main.html index edfd0f6..7490272 100644 --- a/apps/front/templates/layouts/main.html +++ b/apps/front/templates/layouts/main.html @@ -26,24 +26,7 @@
-
- {% for post in posts %} -
-

{{ post.title }}

-

Author: {{ post.author }}

-

-
-
- {% endfor %} -
- + {% include 'partials/blog/all_articles.html' %}
\ No newline at end of file + + // Paginator + pagPrevious = document.querySelector('#pag-previous'); + pagNext = document.querySelector('#pag-next'); + + pagPrevious.addEventListener('click', (event) => { + event.preventDefault(); + document.$CHAT_SOCKET.send(JSON.stringify({ + selector: "#main", + template: "partials/blog/all_articles.html", + data: { + pag: {% if pag > 1 %}{{ pag }} - 1{% else %}1{% endif %} + } + })) + }); + + + pagNext.addEventListener('click', (event) => { + event.preventDefault(); + document.$CHAT_SOCKET.send(JSON.stringify({ + selector: "#main", + template: "partials/blog/all_articles.html", + data: { + pag: {{ pag }} + 1 + } + })) + }); + + + + diff --git a/apps/front/views.py b/apps/front/views.py index d002238..c19583a 100644 --- a/apps/front/views.py +++ b/apps/front/views.py @@ -5,5 +5,6 @@ import uuid def all_articles(request): return render(request, 'layouts/main.html', { "CHANNEL": uuid.uuid4().hex[:20].upper(), - "posts": Post.objects.all()[:5] + "posts": Post.objects.all()[:3], + "pag": 1 }) diff --git a/db.sqlite3 b/db.sqlite3 index cf30280057d526a6030b029e188f2791252e8553..ca8d398c397d5c51055e47dc5c3ca277d572c2d4 100644 GIT binary patch delta 139 zcmZozpx3ZKZ-O+V&O{k!MxBibOZb_^xUwg+3urTOWp7qgFz2$d5@Tf$wRTKOOwKki zFffTvEG@~8H!?6XG%z+aiZ`@0wgho>4UBaSjT8(mt&An*rA{1MUO>KfDy8