【发布时间】:2015-09-17 03:16:58
【问题描述】:
这是我的简单模板:
{% extends "base.template" %}
{% load staticfiles %}
{% block title %}Status{% endblock %}
{% block content %}
{% if status1 %}
{% with "images/"|add:status1|add:".gif" as static_image %}
<img src= "{% static static_image%}" width='100' height='100'/>
{% endwith %}
{% endif %}
{% if status2 %}
{% with "images/"|add:status2|add:".gif" as static_image %}
<img src= "{% static static_image%}" width='100' height='100'/>
{% endwith %}
{% endif %}
{% if status3 %}
{% with "images/"|add:status3|add:".gif" as static_image %}
<img src= "{% static static_image%}" width='100' height='100'/>
{% endwith %}
{% endif %}
{% if status4 %}
{% with "images/"|add:status4|add:".gif" as static_image %}
<img src= "{% static static_image%}" width='100' height='100'/>
{% endwith %}
{% endif %}
{% endblock %}
这是我的看法:
def getStatus(request):
status = get_template("template");
stats = getStats()
if len(stats) == 1:
rtn = status.render(Context({'status':stats[0]}))
elif len(stats) == 2:
rtn = status.render(Context({'status1':stats[0],
'status2':stats[1]}))
elif len(stats) == 3:
rtn = status.render(Context({'status1':stats[0],
'status2':stats[1],
'status3':stats[2]}))
elif len(stats) == 4:
rtn = status.render(Context({'status1':stats[0],
'status2':stats[1],
'status3':stats[2],
'status4':stats[3]}))
return HttpResponse(rtn)
这一切正常。 getStats() (在服务器端)的结果会定期更改,我想更改用户屏幕视图中显示的图像。我宁愿不使用 html 刷新和更新整个页面。我只想更新图像(并且仅在它实际更改时)。
我认为答案是 Ajax...我已经寻找了 Ajax 和 Django 的简单示例,但没有找到任何有意义的东西(对我来说)。任何建议都将受到欢迎。
编辑:
这是我的 urls.py:
from django.conf.urls import patterns, include, url
from garageMonitor.views import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url('time', currentTime),
)
这是我的 base.template:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<h1>Garage Status</h1>
{% block content %}{% endblock %}
{% block footer %}
<hr>
<p>Thanks for visiting my site.</p>
{% endblock %}
</body>
</html>
这是我的时间模板:
{% extends "base.template" %}
{% load staticfiles %}
{% block title %}Current Time{% endblock %}
{% block content %}
Here is time.template
{{ time }}
<onload="refresh()" src="/static/garageMonitor/scripts/refreshTime.js">
{% endblock %}
这是我的views.py:
import datetime
from time import gmtime, strftime
import os
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.http import Http404
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
def currentTime(request):
status = get_template("time.template");
now = strftime("%H:%M:%S", gmtime())
rtn = status.render(Context({'time':now}))
return HttpResponse(rtn)
这是我的 refreshTime.js:
function refresh() {
$.ajax({
url: '{% url garageMonitor/monitor-test %}',
success: function(data) {
blah
$('#test').html(data);
}
setInterval("refresh()", 1000);
});
};
$(function(){
refresh();
});
据我了解(并没有那么多!),文件 /static/garageMonitor/scripts/refreshTime.js 中的 refresh() 函数应该在时间页面加载时调用。然后该函数应该每秒调用一次。
页面加载正确,但没有刷新。我做错了什么?
【问题讨论】: