Coder Social home page Coder Social logo

jonndoe / turbo-django Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hotwire-django/turbo-django

0.0 1.0 0.0 170 KB

An early stage integration of Hotwire Turbo with Django

Home Page: https://discuss.hotwire.dev/t/django-backend-support-for-hotwire/1570/

License: Other

Python 83.26% HTML 9.14% JavaScript 7.59%

turbo-django's Introduction

Build Status Documentation Status Issues Twitter

Turbo for Django

Integrate Hotwire Turbo with Django with ease.

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 turbo-django

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 web page subscribes to a specific broadcast name.

  • A view sends a rendered template to all subscribed pages telling the page where to position the new content.

Example

First, create a view that takes a broadcast name.

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 'broadcast_name' %}

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

Now run ./manage.py shell. Create a Turbo object that references the broadcast name. Tell the object to render a TurboRender object from the string, and then broadcast a command to update the inside of the element with id broadcast_box on all subscribed pages.

from turbo import Turbo
from datetime import datetime

Turbo('broadcast_name').render_from_string(
    f"{datetime.now()}: This is a broadcast."
).update(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

Turbo-Django 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!

turbo-django's People

Contributors

scuml avatar blopker avatar julianfeinauer avatar davish avatar c4ptaincrunch avatar epineda avatar paoloromolini avatar joirichi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.