I am making a website using Django and I want a popup window that displays logging messages and will automatically refresh every N seconds. I am using the standard python logger, javascript, and Dajaxice for my popups.
I am stuck on how to get the popup to refresh automatically, and it needs to use the log file content retrieved in the Dajaxice function.
My ajax.py looks like this:
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})
My proposed Django HTML looks like this:
<button class="btn btn-primary" onclick="Dajaxice.InterfaceApp.getLogs(popitup())">{% bootstrap_icon "share-alt" %} View Log File </button>
The proposed JS looks like this:
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();
}
}
I tried this but am getting an error that the data from my dajaxice function is not defined:
Uncaught TypeError: Cannot read property 'logLines' of undefined
Yet when I call it like this (as a simple alert window) it works:
<input id="LogMessages" type="button" value="View Log Messages" onclick="Dajaxice.InterfaceApp.getLogs(function(d){alert(d.message);})"/>
I am pretty new to JS and dajaxice and there isn't much available online for dajax. Can anyone please help me out with this?
The problem was in how I was calling it. The correct way looks like this:
<button class="btn btn-primary" onclick="Dajaxice.InterfaceApp.getLogs(popitup)">{% bootstrap_icon "share-alt" %} View Log File </button>
There shouldn't have been ()
behind the call to popitup