hagsteel / swampdragon

swampdragon
Other
557 stars 74 forks source link

Idea: add template tag to make template parts update in realtime #195

Closed mback2k closed 5 years ago

mback2k commented 8 years ago

Hello everyone,

I think the following approach would be really cool and easy to use in order to make existing Django websites / templates support live updating.

Assume that you put some special template tag blocks around HTML code like this:

{% swampdragonlive "channel-name" model_instance %}
<p>{{ model_instance.status }}</p>
{% endswampdragonlive %}

The block tags would automatically add some JavaScript magic and store the relevant template context in the backend. On every post_save signal of the model_instance the template part would be rendered and pushed through the "channel-name". The JavaScript code would update corresponding HTML code by just replacing it.

This way the existing Django template code can be re-used and there is no need for client side HTML rendering, for example with AngularJS.

Of course, caching the template context (including the request and user variables) and assigning it to the websocket channel requires some special session handling by the JavaScript and backend code.

mback2k commented 8 years ago

I published my first try on an implementation at https://github.com/mback2k/swampdragon-live. The following commit shows how it can be used: https://github.com/mback2k/django-webgcal/commit/f4c95b1eb2aab01cb427499d6369f2f9cbc7f27e

hagsteel commented 8 years ago

that's a pretty cool idea!

mback2k commented 8 years ago

Thanks. Once SwampDragon is compatible to Django 1.9, I can make use of the new on_commit hook to drop the dependency on Celery. Right now it is required in order to render the updated model state outside of the pending transaction during post_save.

hagsteel commented 8 years ago

I did open it up the other day to do a bit of work on this but it's apparent that I do not have the time to maintain this. I've asked for help in the past but no one has shown any interest.

Would you be interested?

mback2k commented 8 years ago

Sorry, I would be interested, but unfortunately I also do not have enough time to properly maintain such a project.

mback2k commented 8 years ago

I updated SwampDragon-live to require SwampDragon-auth in order to make sure that users are only allowed to subscribe to their specific live template channel. I am also working on the possibility to listen on whole QuerySets instead of just single Model instances.

mback2k commented 5 years ago

This was previously implemented as https://github.com/mback2k/swampdragon-live (including support for QuerySets) and has now been migrated to Channels with https://github.com/mback2k/django-live-templates.