Django通过使用ajax使用模态的更新功能

时间:2018-10-04 02:46:29

标签: django-ajax-selects

我正在使用Django 2.1。我必须使用Bootstrap Modal更新表中列表中的项目。但是,问题是我无法获得列表中项目的ID并将其传递给模式。结果是Modal出现了,但是它是空的

HTML

Changing the translatesAutoresizingMaskIntoConstraints property of the contentView of a UITableViewCell
is not supported and will result in undefined behavior

脚本       

                          <tbody>
                        {% for item_type in item_types %}
                          <tr class="odd gradeX">
                            <td><a href="{% url 'update_item_type' id=item_type.id %}">{{ item_type.item_type_name }}</a></td>
                            <td>{{ item_type.item_type_description }}</td>
                            <td>{{ item_type.is_active }}</td>
                            <td>{{ item_type.id }}</td>
                            <!-- <td><a class="btn btn-info btn-sm pull-right" data-url="{% url 'update_item_type' id=item_type.id %}" class="open-modal" data-toggle="modal" data-target="#myEdit">Update</a></td> -->
                            <td><a class="btn btn-info" class="open-modal" data-url="{% url 'update_item_type' id=item_type.id %}">Edit</a></td>
                            <!-- <td><a class="btn btn-info btn-sm pull-right" class="open-modal" data-url="{% url 'update_item_type' id=item_type.id %}" data-toggle="modal" data-target="#myEdit" style="width:100%;">
                                Update
                            </a></td> -->
                            <!-- <td><button class="btn btn-info btn-sm pull-right" class="open-modal" data-url="{% url 'update_item_type' id=item_type.id %}" data-toggle="modal" data-target="#myEdit" style="width:100%;">
                                Update
                            </button></td> -->
                          </tr>
                        {% endfor %}
                      </tbody>

MODAL

  </div>

  <script>
    var modalDiv = $("#modal-div");

    $(".open-modal").on("click", function(){
      $.ajax({
        type: 'GET',
        url: $(this).attr("data-url"),
        success: function(data){
          modalDiv.html(data);
          $("#myEdit").modal();
        }
      });
    });
  </script>

views.py

      <div class="modal fade" id="myEdit" role="dialog">
    <div class="modal-dialog">
      <!-- <form  class="well contact-form" method="post" action="{% url 'update_item_type'}"> -->
        <form  class="well contact-form" method="post" action="{% url 'update_item_type' id=item_type.id %}">

        {% csrf_token %}
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            {{ item_type_form|bootstrap }}
          </div>
          <div class="modal-footer">
            <button  type="submit" class="btn btn-default">Valider</button>
            <button  value="" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </form>
    </div>
  </div>

0 个答案:

没有答案