django live templates是django和channels的扩展,它增加了对模型更改时动态更新django模板片段的支持。
django-live-templates的Python项目详细描述
Django-Live-Templates是 对Django和 Channels添加 支持在模型更改时实时更新django模板片段。
本项目基于并部分重新实施 SwampDragon-live已生成 使用SwampDragon 与沼泽龙Auth和Django Redis合作。
安装
从pypi.python.org安装最新版本:
pip install django-live-templates
通过从github.com克隆源代码来安装开发版本:
pip install git+https://github.com/mback2k/django-live-templates.git
配置
将包添加到您的INSTALLED_APPS
:
INSTALLED_APPS += (
'channels',
'django_live_templates',
)
示例
确保将django redis用作名为“django live templates”或“default”的缓存后端:
CACHES = {
'django-live-templates': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://localhost:6379/0',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
}
}
}
在django模板中加载所需的javascript模板标记:
{% load django_live_template %}
将所需的javascript添加到django模板:
<script type="text/javascript" src="{{ STATIC_URL }}js/django_live_template.js"></script>
使用include_live template标记而不是默认的include template标记, 如果行是django数据库,则queryset将侦听添加、更改、删除的实例:
{% include_live 'includes/table_body.html' rows=rows perms=perms %}
使用include_live template标记而不是默认的include template标记, row是一个django数据库模型实例,可以监听更改:
{% include_live 'includes/row_cols.html' row=row perms=perms %}
在包含的模板中使用django_live_template变量添加 此模板根标记所需的类,例如第一个标记节点:
<tr class="{{ django_live_template }}">...</tr>
您可以通过以下方式检查内容推送器是否正在实时呈现模板
使用上下文变量is_django_live_template
如下:
{% if is_django_live_template %}
<style onload="alert('Hello World!');"></style>
{% endif %}
一个真实的例子可以在django项目webgcal中找到:
- https://github.com/mback2k/django-webgcal/blob/master/webgcal/apps/webgcal/templates/show_dashboard.html
- https://github.com/mback2k/django-webgcal/tree/master/webgcal/apps/webgcal/templates/includes
许可证
- 根据麻省理工学院许可证发布
- 版权所有(c)2015-2019 Marc Hoerskeninfo@marc-hoersken.de