python - 无限滚动条不适用于 django
问题描述
我问这个问题已经很久了,仍然没有得到答案。我正在尝试使用 Django 添加无限向下滚动,但使用以下代码无法正常工作。我只是将帖子分页 10,然后它只是显示我正在加载图标。当我向下滚动时它不起作用。你们能弄清楚这里有什么问题吗?
视图.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 %}
如果需要更多信息而不是在评论会话中告诉我,我将使用该信息更新我的问题。
解决方案
我错过了加载静态所以通过添加加载
{% load static%}
内容块下方
<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>
推荐阅读
- php - 如何在 laravel 中获得不同的查询结果?
- javascript - 为什么我的递归循环最后两次打印最后一个值?
- javascript - 在元素 Vue vm 中包含脚本标签绑定到抛出模板错误
- ruby-on-rails - button_to 没有打开确认对话框
- javascript - 递归获取返回一个承诺数组而不是值
- reactjs - Redux 分配整数值失败
- sql - 使用 SQL 案例的结果比较并返回另一个案例
- javascript - 将对象格式化为嵌套字段
- angular - 为什么在组件提供者中使用 Angular forwardRef?
- css - 如何确保 CSS 网格中的列数是列数的一个因素?