在Django中将值传递给Bootstrap模态 [英] Passing value to Bootstrap modal in Django

查看:684
本文介绍了在Django中将值传递给Bootstrap模态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次用户想删除数据时,我都会显示一个不错的模式,显示一个删除确认对话框。问题是我的许多视图呈现了模板中的元素列表,每个元素都有其细节以及一个微小的删除红色按钮。当然,在django中,视图将元素列表传递给模板,例如客户端列表,并在如下表格中呈现:

 < table class =table table-striped> 
< thead>
< tr>
< th>电子邮件< / th>
< / tr>
< / thead>
< tbody>
{%为客户端的客户%}
< tr>
< td> {{client.email}}< / td>
< td>
< div class =btn-group>
<! - 删除按钮 - >
< a href =#myModalclass =btn btn-danger btn-minititle =Eliminar>
< i class =icon-trash icon-white>< / i>< / a>
< / div>
< / td>
< / tr>
{%endfor%}
< / tbody>



我想要一个Bootstrap模式每次用户按下删除按钮出现并确认他正在删除一些数据。现在,我设法使模态出现并删除用户,但不是正确的用户,以某种方式它唯一获取或尝试删除列表中的第一个用户。您可以在以下链接中查看我的整个模板:全HTML



最后,我的问题在某种程度上涉及将正确的{{client}}传递给模式,而不是客户端列表中的第一个,我认为这是因为第一个声明的模态



例如,我在表中有3个客户端:

  client1@something.com detele-button 
client2@something.com detele-button
client3@something.com detele-button

无论我按下什么客户端删除按钮,它总是显示与client1数据的模式,如果按确认删除,实际上会擦除它。 / p>

谢谢。



edit1:href不行。

解决方案

找到答案,其实感谢keithxm23。



检查生成的HTML有助于我正确。实际上,每个模态都被正确地产生,问题是每个模态的id属性都被设置为相同,在这种情况下,这个 id =myModal,而删除按钮有 HREF = #myModal。解决方案是将modal id属性设置为整个文档中的唯一值,我设法通过设置模态 id ={{client.pk}}和按钮 href =#{{client.pk}}


I have a nice modal showing a delete confirmation dialog each time user wants to delete data. The problem is many of my views render a list of elements in the template, and each element has its detail as well as a tiny delete red button. Of course in django the view passes a list of elements to the template, for example a list of clients, and they are rendered in a table like the following:

<table class="table table-striped">  
    <thead>  
      <tr>
        <th>Email</th>
      </tr>  
    </thead>  
    <tbody>
      {% for client in clientes %}
        <tr>  
          <td>{{ client.email }}</td>
          <td>
            <div class="btn-group">
              <!-- Delete button -->
              <a href="#myModal" class="btn btn-danger btn-mini" title="Eliminar">
                <i class="icon-trash icon-white"></i></a>
            </div>
          </td>
        </tr> 
      {% endfor %}
    </tbody>  

I would like to have a Bootstrap modal every time user presses on the delete-button to appear and confirm he is deleting some data. Now, I have managed to make the modal appear and delete the user, but not the correct user, modal somehow its only getting, or trying to delete the first user in the list. You can check my entire template with modal in the following link: FULL HTML

In the end, my problem is somehow related to passing the correct {{ client }} to the modal and not the first one on the list of clients, I'm supposing that happens because of the first declared modal on the for and then does not declare it anymore.

For example, I got 3 clients in the table:

client1@something.com         detele-button
client2@something.com         detele-button
client3@something.com         detele-button

No matter what client delete-button I press, it always shows the modal with client1 data, and actually erases it if press confirm delete.

Thank you.

edit1: href was not ok.

解决方案

Found the answer for this, actually thanks to keithxm23.

Checking the generated HTML helped me get this correct. Actually every modal was being correctly generated the problem was that the id property of every modal was set to the same, in this case id="myModal", and the delete button had href="#myModal". the solution was to set the modal id property to a unique value throughout the document, I managed to do that by setting the modal id="{{client.pk}}" and the button href="#{{client.pk}}".

这篇关于在Django中将值传递给Bootstrap模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆