I am currently developing a website using Django and I am in need of a popup window that can display logging messages and automatically refresh itself every N seconds. In order to achieve this, I am utilizing the standard Python logger, JavaScript, and Dajaxice for handling popups.
However, I have encountered a challenge in getting the popup to refresh automatically while utilizing the log file content retrieved within the Dajaxice function.
The code snippet from my ajax.py is as follows:
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})
This is what my proposed Django HTML markup looks like:
<button class="btn btn-primary" onclick="Dajaxice.InterfaceApp.getLogs(popitup())">{% bootstrap_icon "share-alt" %} View Log File </button>
Below is the sample JS code for reference:
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();
}
}
Although, upon implementing the above solution, I encountered an error stating that the data from my dajaxice function is not defined:
Uncaught TypeError: Cannot read property 'logLines' of undefined
Interestingly, when I simplified things and used an alert window it worked fine:
<input id="LogMessages" type="button" value="View Log Messages" onclick="Dajaxice.InterfaceApp.getLogs(function(d){alert(d.message);})"/>
Given my limited experience with JS and dajaxice, I am finding it challenging to find resources online. Any assistance or guidance on resolving this issue would be greatly appreciated!