我希望生成的随机数应该打印在网页上,即HttpResponse(x)
每 5 秒后通过,我已经使用了该time.sleep
功能。
如何在不刷新页面的情况下打印随机生成的值?
这是我的view.py文件
def main(request):
return render(request,'graphs/index.html')
def random_generator(request):
return HttpResponse(randrange(0, 5))
网址.py
from django.conf.urls.defaults import *
urlpatterns = patterns('',
url(r'random/','apps.graph.views.main', name = 'graph_home'),
url(r'random_gen/','apps.graph.views.random_generator', name = 'random'),
)
模板
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function refreshRandom() {
$.ajax({
url: 'random/',
dataType: 'html',
success: function(data) {
$('#random').html(data);
},
complete: function() {
window.setTimeout(refreshRandom, 5000);
}
});
}
window.setTimeout(refreshRandom, 5000);
</script>
</head>
<body>
<div id='random'></div>
</body>
</html>