Skip to main content

Integrate Hotwire Turbo with Django allowing for a Python-driven dynamic web experience.

Project description

Build Status Documentation Status Issues Twitter

Hotwired Turbo for Django

Integrate Hotwire Turbo with Django with ease. This project is fork of turbo-django

Requirements

  • Python 3.8+
  • Django 3.1+
  • Channels 3.0+ (Optional for Turbo Frames, but needed for Turbo Stream support)

Installation

Turbo Django is available on PyPI - to install it, just run:

pip install django-turbojs

Add turbo and channels to INSTALLED_APPS, and copy the following CHANNEL_LAYERS setting:

INSTALLED_APPS = [
    ...
    'turbo',
    'channels'
    ...
]

CHANNEL_LAYERS = {
    "default": {
        # You will need to `pip install channels_redis` and configure a redis instance.
        # Using InMemoryChannelLayer will not work as the memory is not shared between threads.
        # See https://channels.readthedocs.io/en/latest/topics/channel_layers.html
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    }
}

And collect static files if the development server is not hosting them:

./manage.py collectstatic

Note: Both Hotwire and this library are still in beta development and may introduce breaking API changes between releases. It is advised to pin the library to a specific version during install.

Quickstart

Want to see Hotwire in action? Here's a simple broadcast that can be setup in less than a minute.

The basics:

  • A Turbo Stream class is declared in python.

  • A template subscribes to the Turbo Stream.

  • HTML is be pushed to all subscribed pages which replaces the content of specified HTML p tag.

Example

First, in a django app called quickstart, declare BroadcastStream in a file named streams.py.

# streams.py

import turbo

class BroadcastStream(turbo.Stream):
    pass

Then, create a template that subscribes to the stream.

from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('quickstart/', TemplateView.as_view(template_name='broadcast_example.html'))
]
# broadcast_example.html

{% load turbo_streams %}
<!DOCTYPE html>
<html lang="en">
<head>
    {% include "turbo/head.html" %}
</head>
<body>
    {% turbo_subscribe 'quickstart:BroadcastStream' %}

    <p id="broadcast_box">Placeholder for broadcast</p>
</body>
</html>

Now run ./manage.py shell. Import the Turbo Stream and tell the stream to take the current timestamp and update the element with id broadcast_box on all subscribed pages.

from quickstart.streams import BroadcastStream
from datetime import datetime

BroadcastStream().update(text=f"The date and time is now: {datetime.now()}", id="broadcast_box")

With the quickstart/ path open in a browser window, watch as the broadcast pushes messages to the page.

Now change .update() to .append() and resend the broadcast a few times. Notice you do not have to reload the page to get this modified behavior.

Excited to learn more? Be sure to walk through the tutorial and read more about what Turbo can do for you.

Documentation

Read the full documentation at readthedocs.io.

Contribute

Discussions about a Django/Hotwire integration are happening on the Hotwire forum. And on Slack, which you can join by clicking here!

As this new magic is discovered, you can expect to see a few repositories with experiments and demos appear in @hotwire-django. If you too are experimenting, we encourage you to ask for write access to the GitHub organization and to publish your work in a @hotwire-django repository.

License

django-turbojs is released under the MIT License to keep compatibility with the Hotwire project.

If you submit a pull request. Remember to add yourself to CONTRIBUTORS.md!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_turbojs-0.5.0.tar.gz (33.3 kB view details)

Uploaded Source

Built Distribution

django_turbojs-0.5.0-py3-none-any.whl (35.3 kB view details)

Uploaded Python 3

File details

Details for the file django_turbojs-0.5.0.tar.gz.

File metadata

  • Download URL: django_turbojs-0.5.0.tar.gz
  • Upload date:
  • Size: 33.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/22.6.0

File hashes

Hashes for django_turbojs-0.5.0.tar.gz
Algorithm Hash digest
SHA256 871f5b11e4bd327e46a81c7a9f4269fc4826e3b8d39618bfee21a47c16f7f8f8
MD5 b02f6ef605aae6229288035827ba2879
BLAKE2b-256 3bc8a0c9a358f59d333a18dbaa79818fac3c02880cdd067adc0ef86b7fb3c201

See more details on using hashes here.

File details

Details for the file django_turbojs-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: django_turbojs-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 35.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/22.6.0

File hashes

Hashes for django_turbojs-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 93208477928946bd5b435d6ceba79a4c7929635066ac5fe61aa3f55e936cb152
MD5 b2737f26e18c2167a549259ed9e8f67e
BLAKE2b-256 98a5c10be094ef6d2546d6d6e4a2e41b7ef39590eadfa14319d68a2c72ce9fd0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page