繁体   English   中英

如何使用 Z9E13B69D1D2DA927102ACAAAFZ15 在 HTML 页面中插入 Django 模板标签 + HTML 元素

[英]How to insert a Django template tag + HTML element in a HTML page with Javascript

如何使用 Javascript 在其中安全地添加一块带有 Django 模板标签的 HTML? 例如,如果我的模板中有这样的内容:

<div id="some-element">
  <span id="my-tag">{{ my_data }}</span>
</div>
<!-- rest of the html page -->

如果我要使用脚本来编辑 div 的 innerHTML,就像下面的脚本一样,它只会将{{ other-data }}作为字符串读取。 我怎样才能使它成为一个实际的 Django 模板标签?

<script>
  document.getElementById("some-element").innerHTML = 
    `
    <h1>This is may new data {{ other-data }} </h1>
    {% if value %}
      <p>This is an if statement</p>
    {% endif %}
    `
</script>

The Django template thing is not a client side thing, when a request comes to the server, Django renders the HTML file then responds with it, the HTML file gets manipulated on the Server

而且您不需要 Django 来使用条件,JS 已经可以根据条件插入元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM