Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite scroll bar is not working with django

It has been a long time when I asked this question and still didn't get an answers. I am trying to add infinite scroll down with Django but it is not working fine with the following code. I just paginating post by 10 and then its just showing me loading icon .it is not working when i am scrolling down. Can you guys figure it out what is wrong here ?

views.py

class PostListView(ListView):
    model = Post
    context_object_name = 'post_list' 
    paginate_by = 10

    def get_queryset(self):
        return Post.objects.filter(create_date__lte=timezone.now()).order_by('-create_date')

postlist.html

{% extends 'base.html' %}
{% block content %}


 <div class="container">
                <div class="row infinite-container">
                    {% for post in post_list%}
                        <div class="col-md-6 infinite-item">
                            <div class="card mb-4 shadow-sm">
                                <img class="img-thumbnail"  src="{{post.image.url}}"/>
                                <div class="card-body">
                                    <h5>{{post.title}}</h5>
                                    <p class="card-text">
                                        {{post.description|truncatewords:20}}
                                    </p>
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                </div>
                {% if page_obj.has_next %}
                true #this is showing me true it also means that it has next page.
                <a class="infinite-more-link" href="?page={{page_obj.next_page_number}}"></a>
                {% endif %}
                <div class="d-flex justify-content-center" style="display:none;">
                    <div class="spinner-border" role="status">
                        <span class="sr-only">Loading...</span>
                    </div>
                </div>
            </div>

 <script src="/static/js/jquery-2.2.4.min.js"></script>
    <script src="/static/js/jquery.waypoints.min.js"></script>
    <script src="/static/js/infinite.min.js"></script>
    <script>
    var infinite = new Waypoint.Infinite({
        element: $('.infinite-container')[0],
        handler: function(direction) {

    },
    offset: 'bottom-in-view',

    onBeforePageLoad: function () {
    $('.spinner-border').show();
    },
    onAfterPageLoad: function () {
    $('.spinner-border').hide();
    }

    });

    </script>


{% endblock content %}

if more information is require than tell me in a comment session i will update my question with that information.

like image 896
Ahmed Yasin Avatar asked Sep 04 '20 15:09

Ahmed Yasin


People also ask

How is infinite scroll implemented?

Infinite scrolling is when a user reaches the bottom of a page and new content is fetched and loaded so the user can continue to scroll in a relatively seamless experience. This is an alternative to other pagination solutions which use numbered pages or buttons that load more content.

Where can I find the code for simple infinite scroll?

If you want to explore the example, the code is available on GitHub: github.com/sibtc/simple-infinite-scroll We were unable to load Disqus.

What is infinite container in Django pagination?

I won’t get into details about Django pagination because I have an article talking exclusively about it, so if you want to learn more, check this post: How to Paginate with Django. The element identified by the class .infinite-container is the container where the plug-in will load more items.

What do I need to use Django pagination?

Basically we will take advantage of Django’s pagination API and a jQuery plug-in. You will find examples using both function-based views and class-based views. We don’t need anything other than Django installed in the back-end.

What is Django and why should you learn it?

Stay Connected! Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.


2 Answers

I was missing loading the static so load that by adding

{% load static%}

below the content block

<script src="{% static '/static/js/jquery-2.2.4.min.js'%}"></script>
    <script src="{% static '/static/js/jquery.waypoints.min.js'%}"></script>
    <script src="{% static '/static/js/infinite.min.js'%}"></script>
like image 107
Flash Maddy Avatar answered Sep 19 '22 10:09

Flash Maddy


I was missing loading the static so load that by adding

{% load static%}

below the content block

<script src="{% static '/static/js/jquery-2.2.4.min.js'%}"></script>
    <script src="{% static '/static/js/jquery.waypoints.min.js'%}"></script>
    <script src="{% static '/static/js/infinite.min.js'%}"></script>
like image 4
Ahmed Yasin Avatar answered Oct 20 '22 14:10

Ahmed Yasin