Django LiveView

Django LiveView

Build real-time, reactive interfaces with Django using WebSockets: write Python, not JavaScript

Build real-time SPAs with Python, not JavaScript

Django LiveView is a framework for creating real-time, interactive web applications entirely in Python, inspired by Phoenix LiveView and Laravel Livewire. It is built on top of Django Channels.

Build rich, dynamic user experiences with server-rendered HTML without writing a single line of JavaScript. Perfect for Django developers who want real-time features without the complexity of a separate frontend framework.

See it in action

Here's a complete example: a button that loads the latest blog article with a single click.

HTML:

<button
    data-liveview-function="load_latest_article"
    data-action="click->page#run">
    Load Latest Article
</button>

<div id="article-container"></div>

Python:

from liveview import liveview_handler, send
from django.template.loader import render_to_string

@liveview_handler("load_latest_article")
def load_latest_article(consumer, content):
    # Get the latest article from database
    article = Article.objects.latest('published_at')

    # Render with Django templates
    html = render_to_string('article.html', {
        'article': article
    })

    # Send to frontend
    send(consumer, {
        "target": "#article-container",
        "html": html
    })

Result (after clicking the button):

<button
    data-liveview-function="load_latest_article"
    data-action="click->page#run">
    Load Latest Article
</button>

<div id="article-container">
    <article>
        <h2>Understanding Django Channels</h2>
        <p class="meta">Published on Dec 15, 2024 by Jane Doe</p>
        <p>Django Channels extends Django to handle WebSockets,
           long-running connections, and background tasks...</p>
        <a href="/blog/understanding-django-channels/">Read more</a>
    </article>
</div>

That's it! No page reload, no API endpoints, no REST, no GraphQL, no frontend framework. The article appears instantly via WebSocket. Just Python and Django templates working together in real-time.

Key features

  • 🎯 Create SPAs without using APIs: No REST or GraphQL needed

  • 🎨 Uses Django's template system to render the frontend (without JavaScript frameworks)

  • 🐍 Logic stays in Python: No split between backend and frontend

  • 🛠️ Use all of Django's tools: ORM, forms, authentication, admin, etc.

  • Everything is asynchronous by default: Built on Django Channels

  • 📚 Zero learning curve: If you know Python and Django, you're ready

  • 🔄 Real-time by design: All interactions happen over WebSockets

  • 🔋 Batteries included: JavaScript assets bundled, automatic reconnection with exponential backoff

  • 💡 Type hints and modern Python (3.10+)

  • 📡 Broadcast support for multi-user real-time updates

  • 🔐 Middleware system for authentication and authorization

Why Django LiveView?

Benchmarks show Django LiveView delivers the fastest response times among Django interactive frameworks:

Response Time Comparison

FrameworkTechnologyResponse TimeData TransferUse Case
LiveViewWebSockets9.35ms450 bytesReal-time dashboards, collaborative apps
HTMXAJAX16.48ms~37 KBModern UX with minimal JavaScript
UnicornAJAX26.76ms~71 KBComplex forms with reactive state
SSRPOST + redirect47.25ms~8.5 KBSEO-critical pages, traditional CRUD

Django LiveView is approximately 43% faster than HTMX and 80% faster than traditional SSR through persistent WebSocket connectivity.

Technology Comparison

FeatureLiveViewSSRHTMXUnicorn
TransportWebSocketHTTPAJAXAJAX
Update TypeReal-timeFull reloadPartialReactive
Multi-user✅ Broadcast
InfrastructureRedis + ChannelsDjango onlyDjango onlyDjango only

What makes it different?

The key difference is the connection model:

Django LiveView uses a persistent WebSocket connection that stays open between the client and server. This allows bidirectional, real-time communication with minimal latency. Think of it like a phone call: once connected, both sides can talk instantly.

HTMX sends a new HTTP request for each user interaction and updates only part of the page. It's like sending text messages: you send a message, wait for a response, then repeat.

Traditional SSR reloads the entire page with each interaction through a POST request followed by a redirect. It's like hanging up and calling back every time you want to say something.

Architecture send

Architecture receive

How does it work?

Let's illustrate with an example: displaying article number 2.

  1. A WebSocket connection (a channel) is established between the client and the server.

  2. JavaScript sends a message via WebSocket to the server (Django).

    Send string

  3. Django interprets the message and renders the HTML of the article through the template system and the database.

  4. Django sends the HTML to JavaScript via the channel and specifies which selector to embed it in.

    Send JSON

  5. JavaScript renders the received HTML in the indicated selector.

    Place HTML

The same process is repeated for each action: clicking a button, submitting a form, navigating, etc.

Ready to start?

Are you ready to create your first real-time SPA? Let's go to the Quick start.