将 django 模板文件内容传递给 javascript

pass django template file content to javascript

我想将模板文件内容传递到我的 javascript 文件,供以后在其中创建 DOM 元素时使用。 我尝试过的是将它作为带有包含标记的变量传递给 js,如下所示:

<script>const list_item = {% include 'myapp/list_item.html' %}</script>

但这最终得到:

Uncaught SyntaxError: Unexpected token '<'

有什么方法可以将模板文件内容传递给js文件吗?

提前致谢

只看你的样本(不对其他可能的方法提出任何建议来达到预期的结果),它应该是:

<script>const list_item = "{% include 'myapp/list_item.html' %}";</script>

模板文件的内容应该作为字符串包含在内,否则会一直抛出语法错误。