Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Dynamic variable value on HTML with Django

I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django tag as it follows:

**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>

However naturally '{{ placeholder|c_value }}' only outputs the first ever value of 'current_value', which is 0 in this case, and so the source code of index.html ends up being:

**source code after '{{ placeholder|c_value }}'**


<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( 0 );
}
, 1000);
}
mytimer();
</script>

Which is not desired since we would like to print the changing-value of 'current_value' each second.

What is the normal approach for these kind of dynamic texts? Many thanks!

/r/django
https://redd.it/82l23c