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

Django中Ajax请求成功时toastr无法显示,疑与Bootstrap 5冲突

解决Ajax搭配Toastr无显示(Bootstrap 5环境)的问题

核心问题排查与修复步骤

  • 脚本加载顺序错误
    Toastr依赖jQuery运行,你当前先加载了toastr.js再加载jQuery,这会导致Toastr无法初始化。必须先加载jQuery,再加载Toastr脚本:

    {% block extra_js %}
    <script src="https://code.jquery.com/jquery-3.5.1.js" 
          integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" 
          crossorigin="anonymous"></script>
    <script src="toastr.js"></script>
    {% endblock extra_js %}
    
  • 样式加载优先级与路径验证

    1. 检查toastr.css路径是否正确,在浏览器开发者工具「网络」面板确认该文件未返回404;
    2. 将toastr.css放在Bootstrap CSS之后加载,避免Bootstrap样式覆盖Toastr:
      <!-- 先加载Bootstrap CSS -->
      <link href="bootstrap.css" rel="stylesheet"/>
      <!-- 再加载Toastr样式 -->
      <link href="toastr.css" rel="stylesheet"/>
      <link rel="stylesheet" href="{% static 'style.css' %}">
      
  • Toastr配置优化(避免被隐藏)
    Bootstrap 5部分组件(如模态框)层级较高,可能会盖住Toastr提示框,补充配置提升层级并明确显示规则:

    success: function (response) {
        alert("Success");
        // 配置Toastr基础参数
        toastr.options = {
            closeButton: true,
            positionClass: 'toast-top-right',
            zIndex: 99999,
            timeOut: 3000,
            preventDuplicates: true
        };
        toastr.success('Added to Favourite');
    }
    
  • 版本兼容性检查
    确认Toastr为最新稳定版,部分旧版Toastr可能与Bootstrap 5的CSS变量或样式规则冲突,可替换为适配Bootstrap 5的版本。

内容的提问来源于stack exchange,提问作者Dung8466

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 04:32:11