Hello,
I am exploring Turbo Django and as per the doc trying a test broadcast stream using below:
BroadcastStream().update(text=f"{datetime.now()}: This is a broadcast.", id="broadcast_box")
While the above works fine from Django Shell, if I need to use above broadcast in python function, how do I use it and where to please this code in my Django app?
I tried adding it in streams.py, however I don’t see the stream messages on the page?
Ok, so I added the broadcast stream in myapp/views.py and it seem to work but it shows the message momentarily for 2-3 seconds and vanishes likely because its due to nature of the div element which is not a static and is lost during refresh. How do I make the stream messages persistent on the page?
Contents of myapp/views.py
def myindex1(request):
BroadcastStream().append(text="Hello From Broadcast", id="broadcast_box")
return render(request, 'broadcast_example.html')
Contents of html page subscribed to Broadcast stream:
{% load turbo_streams %}
<!DOCTYPE html>
<html lang="en">
<head>
{% include "turbo/head.html" %}
</head>
<body>
{% turbo_subscribe 'myapp:BroadcastStream' %}
<p class="broadcast_box_class" id="broadcast_box"></p>
</body>
</html>