帮助加载和呈现react组件的工具集合
django-react-components的Python项目详细描述
django react组件
django react components是一组工具,用于
与django-react-loader
连用。这个工具目前处于测试阶段。
安装
使用pip安装django react组件:
$ pip install django-react-components
将django_react_components
添加到INSTALLED_APPS
中的settings.py
:
INSTALLED_APPS=(...,'django_react_components',)
您还需要安装其他三个依赖项:
- ^{
} :这个的js对应项 包,用于为要加载到django模板中的django-react-components
提供react组件。 - ^{
} :用于呈现 运行react所需的运行时包。 - ^{
} :webpack使用的依赖项 生成要由django-webpack-loader
使用的统计信息。
用法
渲染react组件
在模板中,可以使用{% react_component %}
模板标记呈现react组件。这样做:
- 从
django_webpack_loader
: 加载模板标记和
render_bundle
标记
{%loadreact_componentfromdjango_react_components%}{%loadrender_bundlefromwebpack_loader%}
- 使用
render_bundle
拉入适当的javascript
<head>
{% render_bundle 'runtime' %}
{% render_bundle 'App' %}
</head>
3A.使用react_component
标记将带有关键字参数的组件呈现为道具
<body>
{% react_component 'App' id='app' prop1=prop1 prop2=prop2 %}
</body>
3A.使用react
/endreact
标记来呈现包含呈现内容的组件。这将作为原始html作为children
属性传递给组件。
<body>
{% react 'App' id='app' %}
<h1>Hello World</h1>
<p>{{ content }}</p>
<a href='{% url 'endpoint' %}'>Link</a>
{% endreact 'App' id='app' %}
</body>
要求
python 3.4-3.7,django 1.11-2.2