【发布时间】:2015-09-26 18:09:05
【问题描述】:
我正在使用 Django 创建一个网站,我想要一个显示日志消息的弹出窗口,并且每 N 秒自动刷新一次。我在弹出窗口中使用标准的 python 记录器、javascript 和 Dajaxice。
我对如何让弹出窗口自动刷新感到困惑,它需要使用在 Dajaxice 函数中检索到的日志文件内容。
我的 ajax.py 看起来像这样:
import json, SIMPLCode, logging, os, sys
from dajaxice.decorators import dajaxice_register
@dajaxice_register(method='GET')
def getLogs(request):
fname = "SIMPLCode/Logs/LOG_2015-07-08.log"
with open(fname,"r") as f:
lines = f.readlines()
lines = lines[-10:]
return json.dumps({'logLines':lines})
我提议的 Django HTML 看起来像这样:
<button class="btn btn-primary" onclick="Dajaxice.InterfaceApp.getLogs(popitup())">{% bootstrap_icon "share-alt" %} View Log File </button>
提议的 JS 看起来像这样:
function popitup(data) {
$(document).ready(function(data) {
var log_file = data.logLines;
var newwindow=window.open('','Log Viewer','height=300,width=500');
newwindow.write(log_file)
});
if(newwindow && !newwindow.closed){
newwindow.location.reload(true);
newwindow.focus();
}
}
我尝试了这个,但收到一个错误,即我的 dajaxice 函数中的数据未定义:
未捕获的类型错误:无法读取未定义的属性“logLines”
然而,当我这样称呼它(作为一个简单的警报窗口)时,它可以工作:
<input id="LogMessages" type="button" value="View Log Messages" onclick="Dajaxice.InterfaceApp.getLogs(function(d){alert(d.message);})"/>
我对 JS 和 dajaxice 还很陌生,网上没有太多可用于 dajax 的东西。谁能帮我解决这个问题?
【问题讨论】:
标签: javascript json dajaxice