微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过django中的AJAX请求传递数据?

我希望在点击页面底部时从数据库中检索数据.

现在,我到目前为止:

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>
...

Basicaly它在开始时加载了5个项目,我试图实现的是,当我点击页面底部时它会再加载1个项目.
因此,jQuery的工作原理是在console.log(‘Test’)工作,并在我的终端中说

“GET /load/ HTTP/1.1” 200 484

这也很好.

我想我不知怎的搞砸了ajax.我不确定.

你可以告诉我,我很高兴,但任何帮助都非常感谢.

解决方法:

使用这样的东西:

import json
from django.http import JsonResponse


def index(request):
    if request.method == 'GET':
        context = {
                'entry_list': Entry.objects.filter()[:5],
            }
        return JsonResponse(json.dumps(context), safe=False)
    else:
        return JsonResponse({"err_msg": "Failed"})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐