【发布时间】:2015-12-18 09:18:58
【问题描述】:
我正在构建一个 Django 项目并尝试使用 AJAX。我需要使用来自 Raspberri Pi 每 5 秒的新数据刷新我的模板。数据存储在 SQLite 数据库中。
我是 Django 新手,对 Ajax 很陌生。我尝试从一些基本的东西开始,这是我的代码:
view.py
from django.http import HttpResponse
from django.template import RequestContext,loader
from .models import Results
def index(request):
return HttpResponse(loader.get_template('mysensor/index.html').render(RequestContext(request,{'latest_results_list':Results.objects.all()})))
def update(request):
return HttpResponse(loader.get_template('mysensor/index.html').render(RequestContext(request,{'latest_results_list':Results.objects.all()})))
我的模板:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script type="text/javascript">
$( document ).ready(function() {
setInterval(function(){
$.ajax({
url:'/mysensor/update' ,
type: "get",
cache: true,
timeout: 30000,
dataType: 'html',
success: function(data) {
console.log("success");
$('#MyTable').html(data);
},
error: function(data) {
alert("Got an error dude "+data);
}
});
},5000);
});
</script>
<table id="MyTable">
<tr>
<th>Date</th>
<th>Time</th>
<th>Temperature</th>
<th>Humidity</th>
</tr>
{% for b in latest_results_list %}
<tr>
<td>{{ b.date }}</td>
<td>{{ b.time }}</td>
<td>{{ b.temperature }}</td>
<td>{{ b.humidity }}</td>
</tr>
{% endfor %}
</table>
在运行时,我的网页崩溃并分析服务器日志,表格并没有像我预期的那样每 5 秒刷新一次,而是崩溃了。
服务器日志片段:
[21/Sep/2015 10:43:46] "GET /mysensor/ HTTP/1.1" 200 7517
[21/Sep/2015 10:43:51] "GET /mysensor/update HTTP/1.1" 301 0
[21/Sep/2015 10:43:51] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:43:56] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:43:56] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:01] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:01] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:01] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:01] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:06] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:07] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
[21/Sep/2015 10:44:11] "GET /mysensor/update/ HTTP/1.1" 200 7517
任何建议将不胜感激。 提前致谢。
【问题讨论】:
-
你只想更新表格,但是ajax请求返回的
data会包含整个html,包括脚本。您要么希望更改模板以仅在执行 ajax 请求时返回表格,要么更改 javascript 以在执行$('#MyTable').html()之前从data提取表格内容。 -
@Alasdair 对,这是我没有考虑到的问题,我会解决它,然后看看会发生什么。顺便问一下,您推荐最好的解决方案是什么?
-
使用 jQuery 提取内容可能是最简单的。请参阅下面的答案。
标签: ajax django setinterval