jquery - modal-body 类中的值仅显示 Django 模型“hostels”的第一个对象
问题描述
modal-body 类中的值仅显示 Django 模型 'hostels' 的第一个对象,但是当此代码中的 'edit' 被替换为 {{hostel.hostel_name}} 时,它工作正常。有什么问题
{% for hostel in hostels %}
<div class="container" style="width: 48%;margin: 10px;height: 140px;background-color: white;display: inline-block;position: relative;">
<a href="{{hostel.pk}}/deletehostel/" style="color: white;position: absolute;top: 0px;right: 0px;margin: 0px;padding: 0px;padding-right: 5px;padding-left: 5px;background-color: red">
X
</a>
<a href="{{hostel.pk}}" style="text-decoration: none">
<h1 style="text-align: center;">{{hostel.hostel_name}}</h1>
<p style="text-align: center;">total beds: {{hostel.capacity}}</p>
</a>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter" style="display: block;margin: auto">edit</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Edit details here</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="text" class="form-control" value="{{hostel.hostel_name}}" name="" placeholder="hostel name">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
解决方案
为避免这种情况,请使用动态 ID。尝试这个:
{% for hostel in hostels %}
<div class="container" style="width: 48%;margin: 10px;height: 140px;background-color: white;display: inline-block;position: relative;">
<a href="{{hostel.pk}}/deletehostel/" style="color: white;position: absolute;top: 0px;right: 0px;margin: 0px;padding: 0px;padding-right: 5px;padding-left: 5px;background-color: red">
X
</a>
<a href="{{hostel.pk}}" style="text-decoration: none">
<h1 style="text-align: center;">{{hostel.hostel_name}}</h1>
<p style="text-align: center;">total beds: {{hostel.capacity}}</p>
</a>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter{{hostel.hostel_name}}" style="display: block;margin: auto">edit</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter{{hostel.hostel_name}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle{{hostel.hostel_name}}" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Edit details here</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="text" class="form-control" value="{{hostel.hostel_name}}" name="" placeholder="hostel name">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
推荐阅读
- python - 我正在使用 google colab 的 Tenserflow 1.x,但为什么我会出错
- python - SSD MobileNet V2 FPNLite 320x320 中的 FPN 代表什么?
- html - 使用 CSS 设置选择标签的样式
- c - 了解具有多个函数调用的函数序言
- python - 如何在 Beautifulsoup 中刮取与标题文本对应的 td
- python - 为什么半硬式在线学习预测 MNIST 需要 256 个输出?
- c# - 如何使用代码将 Crystal Report 连接到 SQL Server
- laravel - Laravel 安排一次性活动
- python - Scrapy 用户代理被阻止或在远程服务器上不起作用
- pine-script - 您将如何修改此 Tradingview Pine 脚本以使最后一个(当前)实时柱像其他柱一样被打印