Package: django-ws-include
django-ws-includeInclude your templates asynchronously and load their contents using websockets
inDjango.See this library in github.How does this work?This library includes javascript code in thews_includetemplate_tag
and a static javascript filews_include.jsfileThat 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 ofdjango-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.InstallationThis package is in pypi.
Use pip to install it:pipinstalldjango-ws-includeInstallation in your Django projectInclude channels and ws_include in your project'ssettings.py:INSTALLED_APPS=[## ...'channels','ws_include',]Create asgi.py file with django-ws-include routesimportosfromchannels.authimportAuthMiddlewareStackfromchannels.routingimportProtocolTypeRouter,URLRouterfromdjango.core.asgiimportget_asgi_applicationfromws_include.routingimportwebsocket_urlpatternsos.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:<scripttype="text/javascript"src="{% static 'ws_include/ws_include.js' %}"></script>UseLoad thews_includetemplate tags at the top of your template and use thews_includetemplate 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 thews_includetemplate 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 "<pathofthe>"<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 limitationsObject dynamic attributesNo dynamic attribute will be passed to the templates given that only a reference to it is passed
from the caller to the included template callee.Don't use dynamic attributes inside an async_included template.However, the full object will be passed to the template,
so you could call its methods and properties without any problem.QuerySetsEach QuerySet is passed as encrypted SQL and converted on the receiver to a RawQuerySet.Note that RawQuerySets have no len method so length filter returns always 0.**Load ws_included_length in your template and use the filterws_included_lengthto get the length.{# Partial HTML template file to be loaded by websockets #}
{% load ws_included_length %}<div><h2>Product list (items|ws_included_length)</h2><ul>{% for item in items %}<li>{{ item.name }}</li>{% endfor %}</ul></div>ExamplesPassing 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 %}CustomizationBlock wrapper html tagWrapper tag isdivand maybe you don't want that. Sethtml__tagoptional 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 classCustomize the wrapper class by passinghtml__tag__classoptional 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' %}TODOMore tests: test the Websocket Consumer code.Improve documentation.Main authorDiego 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.