【发布时间】:2018-08-15 01:01:06
【问题描述】:
我希望在 HTML 文件中显示一个不断变化的 python 变量(从 websocket 服务器读取),目前我正在使用 Django 标记,如下所示:
templatetags/mytag.py
from django import template
register = template.Library()
current_value = 0
@register.filter
def c_value(placeholder):
return current_value
#more code that modifies current_value reading from a websocket server
index.html
{% load mytag %}
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( {{ placeholder|c_value }} );
}
, 1000);
}
mytimer();
</script>
#some code from the website
<span id="stuff">Holder</span>
当然,'{{ placeholder|c_value }}' 只输出'current_value' 的第一个值,在这种情况下为 0,因此 index.html 的源代码最终为:
'{{ placeholder|c_value }}'之后的源代码
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( 0 );
}
, 1000);
}
mytimer();
</script>
这是不希望的,因为我们希望每秒打印 'current_value' 的变化值。
这类动态文本的常规方法是什么?非常感谢!
【问题讨论】:
标签: python html django dynamic tags