上一篇文章我们已经知道如何利用Django弹出简单的消息提示框,文章链接:Django如何弹出简单的消息提示框

但是这样的界面有点丑,界面没有优化,下面介绍如何引入bootstrap的模态框作为自己的消息提示框,你会发现提示框比以前好看多了

1、引入bootstrap框架

最基本的框架,引入细节可以查看官网:https://v4.bootcss.com/docs/getting-started/introduction/

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
  </body>
</html>

2、加入模态框代码

代码如下:

<!-- Modal -->
<div class="modal fade" id="messageModal" tabindex="-1" role="dialog" aria-labelledby="messageModal" aria-hidden="true">
    <div class="modal-dialog" role="document">
         <div class="modal-content">
               <div class="modal-header">
                   <h5 class="modal-title" id="messageModal">提示信息</h5>
                   <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                   </button>
                </div>
                <div class="modal-body" id="message-body">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">确认</button>
                </div>
          </div>
     </div>
</div>

3、JS代码

{% if messages  %}
    <script>
       $(document).ready(function(){
         {% for msg in messages %}
         // 把Django返回的消息内容更新为模态框的内容 通过id号查找
            $("#messageModal").find("#message-body").html('{{ msg.message }}');
          // 显示模态框
            $("#messageModal").modal('show');
         {% endfor %}
       });
    </script>
{% endif %} 

4、测试

效果如下:

0
Posted in 科技, 问题解决

Leave a Comment:

电子邮件地址不会被公开。