简体   繁体   English

如何动态重新渲染模板?

[英]How to dynamically re-render template?

Currently I am trying to create a dynamic filter for listing model objects in a template. 目前,我正在尝试创建动态过滤器,以列出模板中的模型对象。 Here is the django view: 这是django视图:

def view_data(request):
    text = request.GET.get('text')
    persons = None
    if text:
        try:
            persons = models.Person.objects.get(code__regex=text)
        except models.Person.DoesNotExist:
            pass
    return render(request, 'view_data.html',
                  {'persons': persons if not isinstance(persons, models.Person) else [persons]})

The related part from the template: 模板的相关部分:

<div class="jumbotron row">
  <form>
    <label>Alanyok szűrése</label>
    <input id="filter" type="text" placeholder="Keresett alany">
  </form>
</div>

<div class="row">
  <div class="col-4">
    <div class="list-group" id="list-tab" role="tablist">
      {% for person in persons %}
        <a class="list-group-item list-group-item-action" id="list-{{person.code}}" data-toggle="list" href="#" role="tab" aria-controls="{{person.code}}">{{person.code}}</a>
      {% endfor %}
    </div>
  </div>
  <div class="col-8">
    <div class="visualisation content">
        <div class="canvas_div">
          <canvas id="Canvas1" width="540" height="250" style="border:1px solid #202020;">
          </canvas>
        </div>
    </div>
  </div>
</div>

The input field with filter id has a callback on keyup event which sends a request to django with the content of the input field which is used in the view for query. 具有filter ID的输入字段在keyup事件上有一个回调,该事件向django发送一个请求,该请求包含输入字段的内容,该内容在视图中用于查询。 Here is the callback: 这是回调:

$( "#filter" ).keyup(function() {
    $.get("", {text: $('#filter').val()});
});

When I checked it with Pycharm debugger, the render returns the correct html but on the client side the html doesn't change. 当我用Pycharm调试器检查它时,渲染返回正确的html,但是在客户端,html不变。 How to re-render with the new object list? 如何用新的对象列表重新渲染?

Take a part of your html code that you want to replace and place it inside a new html file like this: 取一部分要替换的html代码,然后将其放在新的html文件中,如下所示:

new_html: new_html:

<div class="list-group" id="list-tab" role="tablist">
  {% for person in persons %}
    <a class="list-group-item list-group-item-action" 
       id="list-{{person.code}}" data-toggle="list" 
       href="#" role="tab" aria-controls="{{person.code}}">
       {{person.code}}
    </a>
  {% endfor %}
</div>

now in your view replace the name of your old html file ( that you are rendering ) with the new html file like this: 现在,在您的视图中,用新的html文件替换旧的html文件的名称(正在渲染),如下所示:

 return render(request, 'new_html.html',
             {'persons': persons if not isinstance(persons,models.Person) else [persons]})

and now in your ajax you can dynamically load this new_html like this : 现在在您的ajax中,您可以像这样动态加载此new_html

 $( "#filter" ).keyup(function() {
    $.get("", 
          {text: $('#filter').val()},
          function(data){
              $( "#list-tab" ).replaceWith( data );
          } 
    );
});

You are not doing nothing with the returned data. 您不会对返回的数据做任何事情。 Add a callback function in the get method call. get方法调用中添加一个回调函数。 You get the response from the server in the first argument. 您会从服务器的第一个参数中获得响应。 Use that to hide and show contents on the page. 使用它可以隐藏和显示页面上的内容。 Or you can even replace elements in the DOM. 或者甚至可以替换DOM中的元素。 See jquery.replaceWith . 请参阅jquery.replaceWith

$( "#filter" ).keyup(function() {
    $.get("", {text: $('#filter').val()}, function(response){  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM