python - 如何在 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 对象?
解决方案
为了将信息从客户端代码 (jQuery) 传递到服务器端 (views.py),您需要使用 AJAX。本教程解释了如何传递有关按赞按钮的信息,代码应该相当直接地修改以与您的 Python 和 jQuery 代码集成。
推荐阅读
- c# - 如何根据路径向 JSON 添加新的 JProperty?
- javascript - Twitter 搜索 API-如何在 NODE.JS 中实现高级访问密钥?
- windows - 如何在 Windows 上实现每个应用程序的路由行为?
- c++ - 迭代流缓冲区时如何摆脱“匹配函数调用错误”?
- delphi - 如何在 TMemo 组件中水平居中文本?
- kubernetes - 在 kustomize 中重用 JSON 补丁
- php - 为什么 $pageCount 返回为 0?(使用 FPDI 查找 PDF 中的页数)
- android - 从 Firebase 填充的 recyclerView 的 SearchView 问题
- c - printf 和 gcc -O 选项改变返回值
- sql - 使用 SQL 确定分数提高的用户子集