我对Django和Bootstrap都是新手。我目前正在尝试在我的Django模板中的for循环中创建一个bootstrap模式窗口,它将显示来自模型的一些数据。
因此,我创建了一个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">×</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,所以它应该是正确的)
发布于 2014-09-26 02:01:43
好了,我已经知道哪里出问题了。我试图使用的ID是文本,而不是我认为的数字,最终是如此简单。我最终使用了我的模型的id字段,它工作得很好。感谢你,rajasimon,给我们指明了正确的方向!
https://stackoverflow.com/questions/26032486
复制相似问题