简体   繁体   English

Django 刷新页面而不重新加载整个页面

[英]Django refresh page without reload whole page

I want to refresh the page at certain intervals, for example, every 10 seconds, without reloading the page.我想以特定的时间间隔刷新页面,例如每 10 秒刷新一次,而不重新加载页面。 I'm sending get request to api.我正在向 api 发送获取请求。 My ajax codes refreshes the page well but it's causing problems because it loads all the divs again and again.我的 ajax 代码可以很好地刷新页面,但它会导致问题,因为它会一次又一次地加载所有 div。 How can I make the div-focused refresh delete the same previous div when refreshing?刷新时如何使以div为中心的刷新删除相同的先前div?

part of my div;我的 div 的一部分;

<div id="testdiv" class="row">
<div class="col-md-4">
    {%if testtt > 1000 %}<div class="dashcontrol2 featured">{%else%}<div class="dashcontrol featured">{%endif%}
    <h4>{%if testtt > 1000 %}<span style="color: red;">XX</span><box-icon type='solid' name='badge' color="red"></box-icon>{%else%}<span style="color: green;">XX</span><box-icon type='solid' name='badge-check' color="green"></box-icon>{%endif%} </box-icon></h4>
    <h4>{{testtt|intcomma}}</h4>
   <a target="_blank" href="XX"><h6>KIBANA</h6></a>
  </ul>
 </div>
</div>

url.py; url.py;

url(r'^dashcontrol$', views.dashcontrol, name='dashcontrol'),

ajax; ajax;

<script>
          
    setInterval(function() { 
      $.get("/dashcontrol", function(data, status){ 
        $("body").html(data); 
    });  
}, 15000);
    </script>

view;看法;

def dashcontrol(request):
        url = "http://XX/XX*/_search"
        headers = {'Content-type': 'application/json'}
        params = {XX}
        print('OK')
        response = requests.get(url=url, json=params, headers=headers)
        data = response.json()
        testtt = data['hits']['total']
        print(data['hits']['total'])
        print('OK')
        return render(request, 'dashcontrol.html', {'testtt ':testtt }

one of the possible solution for this is可能的解决方案之一是
hide default duration is 400ms and i do not think the user will notice that.隐藏默认持续时间是 400 毫秒,我认为用户不会注意到这一点。

<script>
          
    setInterval(function() { 
      $.get("/dashcontrol", function(data, status){ 
        $("#testdiv").hide(); //hide the page
        $("#testdiv").append(data); // and after you hide that data append it.
    });  
}, 15000);
    </script>

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

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