Search code examples
djangofeed

How to create a Django post feed that gradually renders posts in batches as you scroll down?


I am trying to make a Django post feed without pagination, but hypothetically, once there are more than a thousand posts, it would be too heavy to always render all of the posts available. So I am trying to find a way to mimic what many other websites with infinite feeds (like Twitter for example) do so that it renders only a batch of posts, and then renders additional ones once you scroll down to the end of the already rendered ones.

I am using a function view instead of the class view.

Thank you in advance for any help.


Solution

  • It sounds like you should have an API to return paginated results from Django side, and in your front end side, fetches say 5-10 posts at a time

    GET /posts/?offset=0&limit=10

    And you can implement a JS function that is hooked to the onScroll event that looks for if you have reached the end of the post already fetched. There are many libraries that do this for you by the way.

    Here's a reference guide for basically exactly what you are looking for too: https://palewi.re/posts/2010/11/07/django-recipe-twitter-style-infinite-scroll/