diff --git a/docs/source/template-tags.md b/docs/source/template-tags.md index 250012c..87bae2d 100644 --- a/docs/source/template-tags.md +++ b/docs/source/template-tags.md @@ -94,11 +94,18 @@ This can help render `turbo-cable-stream-source` in Django template `` is a custom element provided by [turbo-rails](https://github.com/hotwired/turbo-rails/blob/097d8f90cf0c5ed24ac6b1a49cead73d49fa8ab5/app/javascript/turbo/cable_stream_source_element.js), with it, we can send Turbo Stream over the websocket connection and update the page in real time. -```{note} -1. To support Actioncable on the server, please install [django-actioncable](https://github.com/AccordBox/django-actioncable). -2. Please import `` in your JavaScript code. +To import `turbo-cable-stream-source` element to the frontend, there are two ways: + +```html + ``` +Or you can [Jump start frontend project bundled by Webpack](https://github.com/AccordBox/python-webpack-boilerplate#jump-start-frontend-project-bundled-by-webpack) and install it via `npm install` + +After frontend work is done, to support Actioncable on the server, please install [django-actioncable](https://github.com/AccordBox/django-actioncable). + In `routing.py`, register `TurboStreamCableChannel` ```python @@ -111,6 +118,8 @@ cable_channel_register(TurboStreamCableChannel) In Django template, we can subscribe to stream source like this: ```html +{% load turbo_helper %} + {% turbo_stream_from 'chat' view.kwargs.chat_pk %} ``` @@ -119,6 +128,8 @@ In Django template, we can subscribe to stream source like this: Then in Python code, we can send Turbo Stream to the stream source like this ```python +from turbo_response.channel_helper import broadcast_render_to + broadcast_render_to( ["chat", instance.chat_id], template="message_append.turbo_stream.html", diff --git a/src/turbo_response/templatetags/turbo_helper.py b/src/turbo_response/templatetags/turbo_helper.py index 0df941c..9e6bb41 100644 --- a/src/turbo_response/templatetags/turbo_helper.py +++ b/src/turbo_response/templatetags/turbo_helper.py @@ -119,8 +119,8 @@ def __repr__(self): return "<%s>" % self.__class__.__name__ def render(self, context): + from ..cable_channel import TurboStreamCableChannel from ..channel_helper import generate_signed_stream_key, stream_name_from - from ..consumer import TurboStreamCableChannel stream_name_array = [ stream_name.resolve(context) for stream_name in self.stream_name_array