大佬教程收集整理的这篇文章主要介绍了jquery – 如何在返回的AJAX调用上使用django模板标记?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
AJAX的JS:
$(document).ready(function(){ $('#AJAXBox').keyup(function() { var searchedterm; searchedterm = $(this).val(); $.get('/AJAXsearch/',{searchterm: searchedterm},function(data){ $('#result').html(data); }); }); });
python代码的tl; dr基本上是:
def AJAXsearch(request): searchterm = request.GET['searchterm'] result = UserObj.objects.filter(person_name=searchterm) return httpResponse(result)
当我在模板中使用我的html并执行以下操作时:
<div id="result"> {% for person in result %} {{person.property}} {%endfor%} </div>
试试这个:
def AJAXsearch(request): searchterm = request.GET['searchterm'] result = UserObj.objects.filter(person_name=searchterm) return render(request,"path/to/your/template.html",{"result":result})
根据评论,您将重复使用页面的主要代码以及用于生成< div>的代码.块.
在这种情况下,我建议将该块放入单独的文件中,例如“resultlist.html”和including that in your main template,所以:
resultlist.html:
<div id="result" > {% for person in result %} {{person.property}} {%endfor%} </div>
然后在userprofile.html中:
{# Lots of code around the result list #} {% include "resultlist.html" %} {# Lots of code around the result list #}
以上是大佬教程为你收集整理的jquery – 如何在返回的AJAX调用上使用django模板标记?全部内容,希望文章能够帮你解决jquery – 如何在返回的AJAX调用上使用django模板标记?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。