如何在返回的AJAX调用上使用django模板标签? [英] How to use django template tags on returned AJAX calls?

查看:533
本文介绍了如何在返回的AJAX调用上使用django模板标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的AJAX脚本,它在一个名为 AJAXBox 的搜索框中输入一个字符串,并调用一个视图函数,使用过滤器查询数据库,并返回所有的查询符合输入参数的用户对象。当我用django模板标签来遍历查询语言时,它不起作用。我假设是因为我的Javascript调用的输出实际上并不输出查询,而是django模板不能识别的某些类型的字符串。如何解决这个问题,以便我的AJAX调用返回django输出中正常渲染功能的真正django兼容查询器?



AJAX的JS:

  $(文档)。 ready(function(){

$('#AJAXBox')。keyup(function(){

var searterm;
searchingterm = $(this)。 val();
$ .get('/ AJAXsearch /',{searchterm:searchingterm},function(data){
$('#result')。html(data);

});

});
});

python代码的tl; dr基本上是:

  def AJAXsearch(request):
searchterm = request.GET ['searchterm']
result = UserObj.objects.filter(person_name = searchterm)
返回HttpResponse(结果)

当我在我的模板中去我的html并做某事像:

 < div id =result> 
{%for result 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包含在您的主模板所以:



resultlist.html

 < div id =result> 
{%for result in result%}
{{person.property}}
{%endfor%}< / div>

然后在 userprofile.html 中: p>

  {#结果列表中的许多代码#} 
{%includeresultlist.html%}
{#结果列表中的许多代码#}


I have a simple AJAX script that takes an inputted string in a searchfield named AJAXBox and calls a view function that queries the database with a filter and returns a queryset of all User objects that match the entered parameter. When I go to iterate through the queryset with django template tags, it does not work. I assume it is because the output for my Javascript call does not actually output the queryset but some type of string that django template does not recognize. How can I fix this so that my AJAX calls return the true django compatible querysets that the normal render function in django outputs?

The JS for the AJAX:

$(document).ready(function(){

$('#AJAXBox').keyup(function()  {

    var searchedterm;
    searchedterm = $(this).val();
    $.get('/AJAXsearch/', {searchterm: searchedterm}, function(data){
        $('#result').html(data);

      });       

    });  
});

The tl;dr for the python code is essentially:

def AJAXsearch(request):
    searchterm = request.GET['searchterm']
    result = UserObj.objects.filter(person_name=searchterm)
    return HttpResponse(result)

When I go to my html in my template and do something like:

<div id="result">
    {% for person in result %}
        {{person.property}}
    {%endfor%}
</div>

The template tag loop does not do anything. In fact, I cannot manipulate/design the output at all, it is just a normal string of usernames.

解决方案

You aren't calling your template in your view.

Try this instead:

def AJAXsearch(request):
    searchterm = request.GET['searchterm']
    result = UserObj.objects.filter(person_name=searchterm)
    return render(request,"path/to/your/template.html", {"result":result})

Based on the comments you are reusing the main code for your page with the code for generating your <div> block.

In that case I'd recommend pulling that block into a separate file, say "resultlist.html" and including that in your main template so:

resultlist.html:

<div id="result" >
{% for person in result %}
{{person.property}}
{%endfor%} </div>

Then in userprofile.html:

{# Lots of code around the result list #}
{% include "resultlist.html" %}
{# Lots of code around the result list #}

这篇关于如何在返回的AJAX调用上使用django模板标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆