【发布时间】:2018-11-10 21:22:21
【问题描述】:
我想在点击页面底部时从数据库中检索数据。
现在,我到目前为止:
urls.py
urlpatterns = [
url(r'^$', feedViews.index, name='index'),
url(r'^load/$', feedViews.load, name='load'),
]
views.py
def index(request):
if request.method == 'GET':
context = {
'entry_list': Entry.objects.filter()[:5],
}
return render(request,'index.html',context)
else:
return HttpResponse("Request method is not a GET")
def load(request):
if request.method == 'GET':
context = {
'entry_list': Entry.objects.filter()[:1],
}
return render(request,'index.html',context)
else:
return HttpResponse("Request method is not a GET")
index.html
...
<script>
$(window).on("scroll", function() {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
console.log( "TEST" );
$.ajax(
{
type:"GET",
url: "/load",
data:{
},
})
}
});
</script>
...
基本上它会在开始时加载 5 个项目,而我试图实现的是,一旦我到达页面底部,它就会再加载 1 个。 所以 jQuery 工作是因为 console.log('Test') 工作,在我的终端它说
"GET /load/ HTTP/1.1" 200 484
这也很好。
我想我以某种方式搞砸了 ajax。不过我不确定。
您可能会说我是个菜鸟,但我们非常感谢任何帮助。
【问题讨论】:
-
当我们进行 AJAX 调用时,我们必须返回一个 JsonResponse 而不是渲染任何模板。
-
@Shashank 所以我必须修改这一行:return render(request,'index.html',context) ?到底是什么?
-
看看这个:stackoverflow.com/questions/20306981/… 你可以使用 HttpResponse 或 JsonResponse