Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog pagination #346

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Blog pagination #346

wants to merge 2 commits into from

Conversation

jpot
Copy link
Contributor

@jpot jpot commented Apr 1, 2024

Initial version of the blog pagination. This uses a GET parameter "p" for page number (like https://instanssi.org/2024/?p=2 if there are over 10 blog entries)

This gets the basic job of pagination done.

pagination-screenshot

URLs to individual blog entries to be fixed in the future:

Linking to individual blog entry with its id on the URL (like https://instanssi.org/2024/#1) is not very good. If the blog entry is later on another page (because newer entries are added and it falls to another page) then old enough URLs (in social media) will become non-functional when the element with the certain id (1 in this example) does not exist on the first page (default) anymore.

We will need to address this case of linking to an individual blog entry separately with a different view. Then just link to that view's URL instead of using the HTML element id in the URL and the problem goes away.

Another option to fix the case would be to invert the page numbering so the first 10 ids would always be on page nr 1 and not the latest 10 on page nr 1. Then the default page would be whatever was the highest page number. Also the initial page load would need to set the p GET parameter correctly for links to entries to work also in the future (the user would copy the whole thing like ?p=1#1 from the address bar).
However, most likely the first option is still the best and the most logical solution to use in here. We will want to have that "read a single blog post" view anyway.

@jpot jpot requested a review from teistiz April 1, 2024 21:45
@jpot jpot self-assigned this Apr 1, 2024
@jpot jpot requested a review from katajakasa as a code owner April 1, 2024 21:45
@jpot jpot linked an issue Apr 1, 2024 that may be closed by this pull request
try:
page_number = int(page_number)
except ValueError:
page_number = 1
Copy link
Member

@katajakasa katajakasa Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider splitting the request.GET and exception handler to a separate function, get_page_number() or somesuch.

# We will pass this boolean as extra data to the template for simplicity
# It's used to determine whether to render pagination links or not:
needs_pagination = paginator.count > paginator.per_page
return {"entries": entries, "needs_pagination": needs_pagination, "page_range": paginator.page_range, "page_number": current_page.number, "num_pages": paginator.num_pages}
Copy link
Member

@katajakasa katajakasa Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to run black & isort!

In backend/ directory, run:

poetry run black .
poetry run isort .

@katajakasa
Copy link
Member

katajakasa commented Apr 2, 2024

Btw, to navigate to specific post, you could do something like ?pageWith= and then just load from database the count of posts where event matches the instanssi event id, and ID is smaller than or equal to the currently searched blog post. That would give you the count of posts before the post yo want to show, and you could count the page from that. Then you could just browse to correct page and highlight the post.

count = BlogPost.objects.filter(event=event_id).filter(pk__lte=pageWith).count()
page = floor(count / pageSize)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

site: Add paging, pinning and/or permalinks (to separate views) to blog
2 participants