首页 > 解决方案 > 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">&times;</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 %}

标签: jquerydjangodjango-modelsbootstrap-4django-views

解决方案


为避免这种情况,请使用动态 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">&times;</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 %}

推荐阅读