A simple application for Django to include (and fetch) asynchronous templates by loading templates with websocket connections.
Project description
django-ws-include
Include your templates asynchronously and load their contents using websockets in Django.
How does this work?
This library includes javascript code in the ws_include template_tag and a static javascript file ws_include.js file
That javascript code opens a websocket connection (WS_INCLUDE_WEBSOCKET) that will send a websocket message including the encrypted query and the block ID of the template that must be loaded asynchronously.
Later, the server responds with the rendered template and the corresponding block ID. The javascript code uses the block ID to identify were to include the rendered template.
This project is an optimization of django-async-include, project that while has the same philosophy, it uses HTTP requests to get the rendered templates, so it is less efficient than using a lone websocket connection. You are encouraged to take a look to that project to help you understand the aim of this project.
Installation
This package is in pypi. Use pip to install it:
pip install django-ws-include
Installation in your Django project
Include channels and ws_include in your project's settings.py:
INSTALLED_APPS = [
## ...
'channels',
'ws_include',
]
Create asgi.py file with django-ws-include routes
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from ws_include.routing import websocket_urlpatterns
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
)
})
Add the static file to the header of your HTML base template file:
<script type="text/javascript" src="{% static 'ws_include/ws_include.js' %}"></script>
Use
Load the ws_include template tags at the top of your template and use the ws_include template tag as a replacement of the django include template tag.
You have to pass the local context explicitly to the async included templates, so you can pass all variables you need in your included template as named parameters of the ws_include template tag.
{# Load the ws_include template tag at the top of your template file #}
{% load ws_include %}
{# Call the ws_include template tag indicating what objects needs to replace it #}
{% ws_include "<path of the >" <object1_name>=<object1> <object2_name>=<object2> ... <objectN_name>=<objectN> %}
There is also a repository with a full example that also uses django-async-include: django-async-include-example.
Warning and limitations
See django-async-include warnings and limitations.
Examples
Passing an object
{% load ws_include %}
{# .. #}
{# Load the template and informs the board object is required for the included template #}
{% ws_include "boards/components/view/current_percentage_of_completion.html" board=board %}
Passing a QuerySet
{% load ws_include %}
{# .. #}
{% ws_include "boards/components/view/summary.html" board=board member=member next_due_date_cards=next_due_date_cards %}
Customization
Block wrapper html tag
Wrapper tag is div and maybe you don't want that. Set html__tag optional parameter to the name of the tag you need in that particular context.
Example:
{% load ws_include %}
{# .. #}
{# Will be replaced by <li></li> block instead of <div></div> #}
{% ws_include "boards/components/view/last_comments.html" board=board html__tag='li' %}
Block wrapper html tag class
Customize the wrapper class by passing html__tag__class optional parameter to the template tag.
{% load ws_include %}
{# .. #}
{# Will be replaced by <li></li> block instead of <div></div> #}
{# Class last_comments will be added to wrapper class #}
{% ws_include "boards/components/view/last_comments.html" board=board html__tag='li' html__tag__class='last_comments' %}
TODO
- More tests: test the Websocket Consumer code.
- Improve documentation.
Main author
Diego J. Romero-López is a Software Engineer based on Madrid (Spain).
This project is in no way endorsed or related in any way to my past or current employers.
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
File details
Details for the file django-ws-include-0.2.0.tar.gz
.
File metadata
- Download URL: django-ws-include-0.2.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec9cb63fe64daa1a6de5450b602a6d601c4df08200b4af790e841e85378aa09a |
|
MD5 | 5b5aaba4adb4bce8e58581f5aab1a639 |
|
BLAKE2b-256 | 5f59613841a6781a261f0415471cfa080ab5a1d8e4b62a109ea93aff6343a98d |