【问题标题】:Bootstrap modal, django for loop and modelBootstrap modal,django for 循环和模型
【发布时间】:2014-09-25 06:58:07
【问题描述】:

我对 Django 和 Bootstrap 都很陌生。我目前正在尝试让一个引导模式窗口在我的 Django 模板中的 for 循环内工作,该窗口将显示来自模型的一些数据。

因此,我创建了一个 for 循环,它涵盖了按钮创建和模态窗口,并迭代了我的模型中包含的数据。因此,它应该创建与我的模型中的条目一样多的按钮和相应的模式窗口。

但是,当我单击通过 for 循环创建的任何按钮时,模式窗口始终显示第一个条目并且永远不会更改。

我试图找到答案,我主要研究了以下主题:

Passing value to Bootstrap modal in Django

Twitter bootstrap remote modal shows same content everytime

当我创建与唯一 ID 相关的不同模式和按钮时,这在循环之外完美工作,但我无法使其在我的情况下工作。

我希望足够精确。

感谢您的帮助。

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Remote file for Bootstrap Modal</title>  

  <!--CSS-->
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

  <!--jquery-->
  <script src="http://code.jquery.com/jquery.min.js"></script>

  <!--JS-->
  <script src="http://getbootstrap.com/dist/js/bootstrap.js"></script>


</head>
<body>

          <div class="row">
            {% for name in activities %}
          <div class="col-md-3">
            <div class="thumbnail">
              <img src="#" >
            </div>
            <p>Location: {{name.aregion}}</p>
            <p>

              <!-- Button trigger modal -->
              <button class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal">
                Read More
              </button>

              <!-- Modal -->
              <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                      <h4 class="modal-title" id="myModalLabel">{{name.aname}}, {{name.aregion}}, {{name.acountry}}</h4>
                    </div>
                    <div class="modal-body">
                      ...
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      <button type="button" class="btn btn-primary">Save</button>
                    </div>
                  </div>
                </div>
              </div>
            </p>
          </div>
          {% endfor %}
          </div>

</body>
</html>

(“按钮触发模态”和“模态”代码来自bootstrap.com所以应该是对的)

【问题讨论】:

  • 因为你的id是一样的。不断改变你的id或者jquery可以帮你解决

标签: django twitter-bootstrap bootstrap-modal


【解决方案1】:

好的,我知道出了什么问题。我尝试使用的 ID 是文本,而不是我认为的数字,最终如此简单。我终于使用了我的模型的 id 字段,它工作得很好。谢谢你,rajasimon,你给了正确的方向!

【讨论】:

  • 你做了什么来修复这个错误?我也面临同样的问题
猜你喜欢
  • 2022-11-10
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 2016-02-02
  • 2021-10-17
  • 2020-10-14
相关资源
最近更新 更多