首页 > 解决方案 > 如何在 Django 中与 jQuery 进行通信?

问题描述

我在

index.html中得到了这个脚本

    <script>
       $(window).on("scroll", function() {
           if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
             alert("Bottom of page!");
           }
       });
    <script>

这在我的

视图.py

def entry(request,
    template='index.html',
    page_template='entry_list_page.html'):
    context = {
        'entry_list': Entry.objects.filter()[:4],
        'page_template': page_template,
    }
    if request.is_ajax():
        template = page_template
    return render(request, template, context)



如何让 Django 知道 jQuery 已经到达页面底部,以便我可以再显示 4 个 Entry 对象?

标签: pythonjquerydjango

解决方案


为了将信息从客户端代码 (jQuery) 传递到服务器端 (views.py),您需要使用 AJAX。教程解释了如何传递有关按赞按钮的信息,代码应该相当直接地修改以与您的 Python 和 jQuery 代码集成。


推荐阅读