Requirements
Python 3.10+
Django 4.2+
Redis (for Channels layer)
Channels 4.0+
Installation
Install Django LiveView with pip:
pip install django-liveview
Configure Django
Add to your settings.py:
# settings.py
INSTALLED_APPS = [
"daphne", # Must be first for ASGI support
"channels",
"liveview",
# ... your other apps
]
# ASGI configuration
ASGI_APPLICATION = "your_project.asgi.application"
# Configure Channels with Redis
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
Setup ASGI routing
Create or update asgi.py:
# asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
from channels.security.websocket import AllowedHostsOriginValidator
from liveview.routing import get_liveview_urlpatterns
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
get_liveview_urlpatterns()
)
)
),
})
Add JavaScript to your base template
<!-- templates/base.html -->
{% load static %}
<!DOCTYPE html>
<html lang="en" data-room="{% if request.user.is_authenticated %}{{ request.user.id }}{% else %}anonymous{% endif %}">
<head>
<meta charset="UTF-8">
<title>{% block title %}My Site{% endblock %}</title>
</head>
<body data-controller="page">
{% block content %}{% endblock %}
<!-- Django LiveView JavaScript -->
<script src="{% static 'liveview/liveview.min.js' %}" defer></script>
</body>
</html>
Important attributes:
data-roomon<html>— unique identifier for WebSocket room (user-specific or shared)data-controller="page"on<body>— activates the Stimulus controller
We strongly recommend that you follow the Quick start to see the installation in action.