转换你的角度索引.html文件到Django模板语法
ng2django的Python项目详细描述
ng2django公司
这是一个简单的Python工具,可以将Angular2+构建的HTML文件转换为Django模板语言。这样,静态文件就可以提供给与Django项目一起提供的Agnular应用程序。在
剧本主要做两件事:
- 将
{% load static %}
标记添加到文件顶部 - 将
link
和script
标记的href
和src
属性的值更改为{% static "<path-to-the-file>" %}
依赖关系
脚本使用Beutifulsoup4。您可以通过运行pip install -r requirements.txt
来安装它。在
安装
只需运行$ pip install ng2django
使用
构建你的Angular应用程序并将文件放入<Django-project-root>/<app>/static/<optional-subdirectory>/
。在
脚本接受两个位置参数:
source
:要转换的HTML文件的路径。在dest
:保存Django模板文件的位置。在
然后你就可以跑了
$ ng2django <Django-project-root>/<app>/static/index.html <Django-project-root>/<app>/templates/index.html
如果您将文件直接存储在Django应用程序的static
和{
$ ng2django <Django-project-root>/<app>/static/<optional-subdirectory>/index.html <Django-project-root>/<app>/templates/<optional-subdirectory>/index.html -s <optional-subdirectory>
如果您正在使用这些路径的某个子目录。在
可选参数
Short | Long | Description |
---|---|---|
-s ^{ | --subdir ^{ | Include the subpath under ^{ |
-css ^{ | --cssdir ^{ | Include the subpath under ^{ |
-js ^{ | --jsdir ^{ | Include the subpath under ^{ |
-img ^{ | --imgdir ^{ | Include the subpath under ^{ |
-n | --nodelete | Do not delete the source file after converting. |
-p | --pretty | Create a dest file that is more human readable. |
建议的设置和使用方法
在角度项目中,打开package.json
文件。在scripts
下,将build
更改为
ng build --prod --output-path <Your-django-project-root>/<Django-App>/static/ng
。在
在script
下添加一个postbuild
键,并将值设置为
ng2django <Your-django-project-root>/<Django-App>/static/ng/index.html <Your-django-project-root>/<Django-App>/templates/ng/index.html -s ng
。在
或者,如果在静态参数中的单独目录中有css/js/images:-css css-js-img img
ng2django <Your-django-project-root>/<Django-App>/static/ng/index.html <Your-django-project-root>/<Django-App>/templates/ng/index.html -s ng -css css -js js -img img
。在
运行npm run build
,你的角度JS和CSS文件将被保存到Django应用程序的静态目录中。然后,这个脚本将运行并转换HTML文件并将其保存在Django应用程序的templates目录中。在
贡献
欢迎投稿-提交问题/拉请求。在
推送到PyPi
使用这些命令在PyPi上发布
- 项目
标签: