简体   繁体   English

如何在点击事件上使用 Javascript 在 HTML Div 内重新运行 Django For 循环

[英]How can I re-run a Django For Loop inside a HTML Div using Javascript on Click Event

I have a HTML div like this,我有一个像这样的 HTML div,

<div  class="coment-area ">
            
            <ul class="we-comet">
              {% for j in comment %}
              <div  id="delete_comment{{j.id}}" class="mt-3">
                {% if j.post_id.id == i.id %}
                
                <li >
               
                      <div class="comet-avatar">
                        {% if j.user_id.User_Image %}
                          <img class="card-img-top" style=" vertical-align: middle;width: 50px;height: 50px;border-radius: 50%;" src= {{ j.user_id.User_Image.url }} alt="">
                        {% else %}
                       <img class="card-img-top" style=" vertical-align: middle;width: 60px;height: 60px;border-radius: 50%;" src="static\images\resources\no-profile.jpg">
                       {% endif %}
                      </div>

Inside of it is a For Loop that is executed when the page is first loaded.它的内部是一个在页面首次加载时执行的 For 循环。

Below this For Loop is a Comments Button在这个 For 循环下面是一个评论按钮

    <div >
                                <button type="submit" onclick="comment_save(event,{{i.id}})"  class= "my-2 ml-2 px-2 py-1 btn-info  active hover:bg-gray-400 rounded ">Comment</button>
                            </div>
                            
                        </div>    
                    </li>
                </ul>
            </div>

Whenever this button of Comments is clicked, a function in Javascript is called which is defined below,每当单击此评论按钮时,都会调用 Javascript 中的 function ,其定义如下,

function comment_save(event,post_id)
            
            {   
                var comment_value=$("#comment_value"+post_id).val();    
                

                var user_id=$("#comment_user_id"+post_id).val()
                
                postdata={
                    "comment_value":comment_value,
                    "user_id":user_id,
                    "post_id":post_id
                }
                SendDataToServer("readmore",postdata,function()
                {
                    alert()
                }) 
                
                $("#comment_value"+post_id).val(" ")
                <! --- document.location.reload().. Something here that refresh that for loop --->
                
                
                
            }

What I want is this that whenever the button is clicked, it re-executes that for Loop inside my main div without having to refresh the page.我想要的是,每当单击按钮时,它都会在我的主 div 内重新执行for Loop,而无需刷新页面。 I have been trying to do this for two days but could not find any solution to this.我已经尝试了两天,但找不到任何解决方案。 Can anyone help me in doing this?谁能帮我做这件事?

The Django Templating Language is executed server-side. Django 模板语言在服务器端执行。 That means the client (browser, running Javascript) has no access to it whatsoever.这意味着客户端(浏览器,运行 Javascript)无法访问它。

Some of your options are:您的一些选择是:

  • Do everything back-end: Re-render the template (which you said don't want to do).做所有后端:重新渲染模板(你说不想做)。
  • Do everything front-end: You could have your view function return the data used in your template loop and re-implement it in your front-end (but that makes the original template loop kind of pointless).在前端做所有事情:您可以让您的视图 function 返回模板循环中使用的数据并在您的前端重新实现它(但这使得原始模板循环毫无意义)。
  • Hybrid: Return the data for only the new comment in your response and add it to the list with Javascript.混合:仅返回响应中新评论的数据,并使用 Javascript 将其添加到列表中。

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

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