PacktPublishing / Django-4-by-example

Django 4 by example (4th Edition) published by Packt
https://djangobyexample.com/
MIT License
818 stars 461 forks source link

Question about adding AJAX paginator to activity stream #22

Closed gavin-dev21 closed 1 year ago

gavin-dev21 commented 1 year ago

Hello, on page 312 it states that you can add the AJAX paginator that you used in the image_list view to the activity stream. I'm wondering how that is done? Do you create a whole new view in actions in order to allow infinite pagination? Or is there a way to use the existing code to implement the same feature but for the activity stream? Any help would be appreciated!

zenx commented 1 year ago

@gavin-dev21 you can use the existing dashboard view to reuse the QuerySets. You could do the following:

  1. Replicate the code from the image_list view in the existing dashboard view to implement a Paginator
  2. Get the page number as a GET parameter, and use a variable similar to images_only to differentiate a whole page request from an AJAX request.
  3. Render the complete dashboard.html template for the whole page request or render a new action/list.html template for AJAX requests.
  4. Create the new action/list.html template and use the {% for action in actions %} ... {% endfor %} loop from the dashboard.html template to render actions.
  5. Add the infinite-scroll pagination JavaScript code to the dashboard.html template
gavin-dev21 commented 1 year ago

Perfect, thank you! I got it to work.