介绍:我正在使用以下链接为我的项目添加无限滚动https://simpleisbetterthancomplex.com/tutorial/2017/03/13/how-to-create-infinite-scroll-with-django.html
以下是该链接的 github 代码 https://github.com/sibtc/simple-infinite-scroll
该链接显示了如何将无限滚动添加到function based views和class based views。我已将此添加到我的基于函数的视图中,并且效果很好。
问题:我的帖子列表中有8 个帖子是class-based view. 添加后我paginate_by = 3只能看到 8 个帖子中的 3 个帖子。每次我向下滚动这 3 个帖子时,都会无限循环地重复
我的观点:
class Postlist(SelectRelatedMixin, ListView):
model = Post
select_related = ('user', 'group')
paginate_by = 3
context_object_name = 'post_list'
template_name = 'posts/post_list.html'
def get_queryset(self):
queryset = super(Postlist, self).get_queryset().order_by('-created_at')
query = self.request.GET.get('q')
if query:
queryset = queryset.filter(
Q(title__icontains=query)|
Q(user__username__iexact=query)|
Q(user__first_name__iexact=query)|
Q(user__last_name__iexact=query)
)
return queryset
我的 Base.html :(我在 JS 文件夹中有以下文件,它们为我的 FBV 工作)
<script src="{% static 'js/jquery-3.1.1.min.js' %}"></script>
<script src="{% static 'js/jquery.waypoints.min.js' %}"></script>
<script src="{% static 'js/infinite.min.js' %}"></script>
{% block javascript %}{% endblock %}
我的 post_list.html:
<div class="col-md-8">
<div class="infinite-container">
{% for post in post_list %}
<div class="infinite-item">
{% include "posts/_post.html" %} <!---This code is good---->
</div>
{% empty %}
some code
{% endfor %}
</div>
<div class="loading" style="display: none;">
Loading...
</div>
{% if page_obj.has_next %}
<a class="infinite-more-link" href="?page={{ post_list.next_page_number }}">More</a>
{% endif %}
</div>
相关分类