python - Bootstrap 模态不更新模态内容

标签 python django twitter-bootstrap forms bootstrap-modal

我正在尝试创建一个模型表,模型旁边有一个按钮,该按钮打开一个模式并在表单 View 中具有相同的模型行。该表已正确填充,但正在创建的 n 个 bootstrap modals 仅包含第一个可迭代模型值。是因为 Bootstrap 在呈现页面时仅加载模态内容一次吗?我该怎么做才能解决这个问题?我是否应该运行一个函数来根据它具有的模型数据更新模态内容??

如有任何疑问,请随时提出。

{% extends 'base.html' %}

{% load static %}

{% block content %}
    <table>
        {% for item in data %}

            <tr>
                <th>From</th>
                <th>To</th>
                <th>Weight</th>
                <th>Length</th>
                <th>Type</th>
                <th>Material Type</th>
                <th>Number of Trucks</th>
                <th>Loading Time</th>
            </tr>
            <tr>
                <td>{{ item.From }}</td>
                <td>{{ item.To }}</td>
                <td>{{ item.Weight }}</td>
                <td>{{ item.Length }}</td>
                <td>{{ item.Type }}</td>
                <td>{{ item.MaterialType }}</td>
                <td>{{ item.Numberoftrucks }}</td>
                <td>{{ item.Loadingtime }}</td>
                <td>
                    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Bid
                        now! for id {{ item.id }} </button>
                </td>
                {#        {% endfor %}#}


                <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                    <div class="modal-dialog" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                        aria-hidden="true">&times;</span></button>
                                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                            </div>
                            <div class="modal-body">

                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.To }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.From }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Weight }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Length }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Type }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.MaterialType }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Numberoftrucks }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here..." value="{{ item.Loadingtime }}" disabled>
                                <input class="form-control" id="disabledInput" type="text"
                                       placeholder="Disabled input here...">Bid
                            </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 changes</button>
                            </div>

                        </div>
                    </div>
                </div>

            </tr>
        {% endfor %}
    </table>

{% endblock %}

最佳答案

您可以通过将 model.pk 添加到模态 id 来修复它

在按钮中

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal"
     data-target="#myModal{{ item.id }}">
    Bid now! for id {{ item.id }}
</button>

 <div class="modal fade"
      id="myModal{{ item.id }}"
      tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

其他解决方案是创建 js 函数以在每次激活模式时加载新数据。

关于python - Bootstrap 模态不更新模态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55379511/

相关文章:

python - 具有三层深度嵌套模型的查询集过滤器(多个一对多关系)

django - 数据库连接限制是什么意思?

twitter-bootstrap - 无法在 twitter Bootstrap 中调用未定义错误的方法 'charAt'

twitter-bootstrap - 投影效果不显示 [CSS3, Twitter Bootstrap 3]

python - Pandas 向 MySQL 中插入数据

python - Beautiful Soup - 打印容器文本而不打印子元素的文本

html - 使用文本溢出 : ellipsis with flexbox

php - 想要在php中的bootstrap slider 中制作动态网格

python日期时间和日期比较

javascript - Facebook 连接帮助