Add paginator

This commit is contained in:
Andros Fenollosa 2021-11-12 22:16:42 +01:00
parent 7f65835db7
commit b8b101b466
4 changed files with 29 additions and 11 deletions

6
TODO
View File

@ -1,3 +1,3 @@
- Template base - Single talk.
- change about - List profiles
- Single talk. - Chat

View File

@ -20,7 +20,8 @@ class WebsiteConsumer(AsyncWebsocketConsumer):
await self.channel_layer.group_send( await self.channel_layer.group_send(
self.room_group_name, self.room_group_name,
{ {
"type": "send_page_talks" "type": "send_page_talks",
"page": 1,
} }
) )
@ -36,10 +37,14 @@ class WebsiteConsumer(AsyncWebsocketConsumer):
if data["action"] == "page": if data["action"] == "page":
# Talks # Talks
if data["value"] == "talks": if data["value"] == "talks":
page = 1
if "page" in data:
page = int(data["page"])
await self.channel_layer.group_send( await self.channel_layer.group_send(
self.room_group_name, self.room_group_name,
{ {
"type": "send_page_talks" "type": "send_page_talks",
"page": page,
} }
) )
# About # About
@ -53,12 +58,12 @@ class WebsiteConsumer(AsyncWebsocketConsumer):
# Pages # Pages
def _get_talks(self): def _get_talks(self, page):
return page_talks() return page_talks(page=page)
async def send_page_talks(self, event): async def send_page_talks(self, event):
''' Send Home page ''' ''' Send Home page '''
html = await sync_to_async(self._get_talks)() html = await sync_to_async(self._get_talks)(event["page"])
await self.send(text_data=html) await self.send(text_data=html)
def _get_about(self): def _get_about(self):

View File

@ -17,4 +17,13 @@
</article> </article>
</a> </a>
{% endfor %} {% endfor %}
{# Pagination #}
<div class="loading" >
<form>
<input type="hidden" name="action" value="page">
<input type="hidden" name="value" value="talks">
<input type="hidden" name="page" value="{{ next_page }}">
<p hx-ws="send" hx-trigger="click,revealed">Show more</p>
</form>
</div>
</main> </main>

View File

@ -14,9 +14,13 @@ def index(request):
}) })
def page_talks(): def page_talks(page=1):
return render_to_string("pages/talks.html", { TALK_PER_PAGE = 5
"talks": Talk.objects.order_by("title")[:5] return render_to_string("pages/talks.html",
{
"talks": Talk.objects.order_by("title")[:TALK_PER_PAGE * page],
"page": page,
"next_page": page + 1,
}) })