简体   繁体   English

使用 javascript 在模板中添加 django for loop

[英]add django for loop in the template using javascript

can i add a {% for in %} loop and {{ variables }} in django template using javascript dom (insertAdjacentText, or textContent) and charge data from the views something like refresh only the part of html that containt the added for loop or refresh the page without loosing the javascript execution?我可以使用 javascript dom(insertAdjacentText 或 textContent)在 django 模板中添加 {% for in %} 循环和 {{ variables }} 并从视图中收取数据,例如仅刷新包含 ZFC35FDC70D5FC69D269883A 的部分添加的 ZFC35FDC70D5FC69D2698883A 或循环在不丢失 javascript 执行的情况下刷新页面? if yes guide me i searched and i did not如果是,请指导我,我搜索但我没有

 function displayMaquette(){ let label = document.getElementById('label'); let rem = document.getElementById('remove') let amg = document.getElementsByClassName('amg') let namep =document.getElementsByClassName('namep') let add =document.getElementById('add') add.insertAdjacentText('afterbegin','{% for plan in plan3 %}') add.insertAdjacentText('beforeend','{% endfor %}')*/ for(let i =0;i<x.length;i++){ x[i].textContent='{{ plan.IdPlans }}' amg[i].setAttribute('src','{{plan.Image.url}}') namep[i].textContent='{{ plan.NamePlan }}:' } }
 <figure id="label3" class="fig" onclick="displayMaquette()"> <figcaption class="caption" onmouseover="figCaptionHover(this)" onmouseout="figCaptionOut(this)">Cliquez pour afficher les maquettes</figcaption> <img class="figimg" src="" alt="Elephant at sunset" > </figure> </div> <article class="works none"> <p class="pNone" style="display: none;"></p> <div class="worksimg"> <img class="Img amg" Img src=""> </div> <div class="Description"> <h2 class="namep"></h2> <p></p> </div> </article> <div id="remove"></div>

find?寻找? By onclick event i add the for loop and {{ variables }} but it display as text in the browser it doesn't charge datas from views what i wan't is charge data from database通过 onclick 事件,我添加了 for 循环和 {{ variables }} 但它在浏览器中显示为文本,它不会从视图中收取数据我不希望从数据库中收取数据

It sounds like the javascript you're referring too is running on the client's computer.听起来您所指的 javascript 也在客户端计算机上运行。 If that is the case then no this is not possible.如果是这样,那么不,这是不可能的。 Django templates are evaluated by the server before it sends a response to the client. Django 模板在向客户端发送响应之前由服务器评估。 The client has no way of knowing what django tags are and how to use them since that is a feature built into django itself.客户端无法知道 django 标签是什么以及如何使用它们,因为这是 django 本身内置的功能。

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

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