You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django项目中Bootstrap模态框点击触发无响应问题求助

问题核心原因
  • 你项目中引入的是Bootstrap 4.3.1版本,但复制的是Bootstrap 5.1版本的模态框示例代码,两个大版本的模态框触发属性规则不兼容:
    • Bootstrap 5使用带bs后缀的属性:data-bs-toggle、data-bs-target、data-bs-dismiss
    • Bootstrap 4使用无后缀的属性:data-toggle、data-target、data-dismiss
  • 额外存在的隐患:base.html中重复引入了jQuery,先加载了slim版又加载了完整版,可能引发JS冲突,建议只保留一个jQuery版本。
修复方案

修改main.html里的模态框相关属性,适配Bootstrap 4的规则,修改后代码如下:

{% extends "main/base.html" %}
{% block content %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </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>
{% endblock content %}

同时建议删除base.html中多余的jQuery引入,比如删掉jquery-3.3.1.slim.min.js那一行,避免JS冲突。

内容的提问来源于stack exchange,提问作者オパラ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 11:54:04