簡體   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