如何刷新Django模板中的表(How to refresh a table in template of Django)

编程入门 行业动态 更新时间:2024-10-19 00:27:50
如何刷新Django模板中的表(How to refresh a table in template of Django)

我目前正从我的视图中获取一个对象,并以表格的形式将其显示在模板中。 目前,我很震惊我们需要刷新表/ div的部分,而不刷新页面。

在我的views.py中

def foo(request): testruns...... render_to_response('pages/bar.html', locals(), context_instance=RequestContext(request))

我的bar.html(模板)

<div id = "roman"> {% for trun in testruns %} <tr> <td>{{ trun.testprofile }}</td> <td>{{ trun.time }}</td> <td>{{ trun.testresult }}</td> <td>{{ trun.state }}</td> </tr> {% endfor %} </div>

应该有两种方法可行:

使用dajaxice

使用[Jquery]

$.ajax({ url: '{% url myview %}', success: function(data) { $('#the-div-that-should-be-refreshed').html(data); } });

我想知道哪种方法更适合我的情况。 使用方法2,表格会自动刷新,我们如何设置刷新时间?

I am currently obtaining an object from my views and displaying it in the template in form of a table. Currently I am struck at the part that we need to refresh the table/div, without refreshing the page.

In my views.py

def foo(request): testruns...... render_to_response('pages/bar.html', locals(), context_instance=RequestContext(request))

My bar.html (template)

<div id = "roman"> {% for trun in testruns %} <tr> <td>{{ trun.testprofile }}</td> <td>{{ trun.time }}</td> <td>{{ trun.testresult }}</td> <td>{{ trun.state }}</td> </tr> {% endfor %} </div>

There are two approaches which are supposed to work:

Using dajaxice

Using [Jquery]

$.ajax({ url: '{% url myview %}', success: function(data) { $('#the-div-that-should-be-refreshed').html(data); } });

I would like to know which approach is more suitable to my case. Using Approach 2, would the table auto refresh, and how do we set the time to refresh ?

最满意答案

第二种方法的可接受但缺点(从django拉表作为html):

通过网络传输的数据要大得多

如果您在表中使用基于javascript的组件(可能是基于dojo的按钮等),他们可能会遇到一些问题。 我在dojo中遇到了一个微笑的问题,我在make dojo中找到了解压缩应用的html的解决方案。 但每次生活都不容易,所以第一种方法更好。

Both acceptable but disadvantages of the second approach(pulling table from django as html ):

Carried data over network is much bigger

If u use something javascript based components in your table (maybe dojo based buttons etc.) they may couse some problems. I had a smilar issue in dojo and i found the solution in make dojo reparse the applied html. But life could not be easy everytime so first approach is better.

更多推荐

本文发布于:2023-07-17 13:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144940.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   Django   refresh   template   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!