django messages 实现配合bootsrap 美化弹出框

要在Django中使用messages实现配合Bootstrap美化弹出框,可以按照以下步骤进行操作:

  1. 首先确保你的Django项目中已经集成了Bootstrap库。

  2. 在需要显示消息的视图中,使用Django的messages模块来添加消息。例如:

    from django.contrib import messages
    
    def my_view(request):
        messages.success(request, 'This is a success message.')
        messages.error(request, 'This is an error message.')
        return render(request, 'my_template.html')
     
    

  3. 在模板文件(比如my_template.html)中,使用Bootstrap的样式来美化消息弹出框。可以使用Bootstrap提供的Alert组件来显示消息。例如:
    <style>
      .alert {
                position: fixed;
                width:300px;
                top: 40px;
                margin-left: 50%;
                transform:translateX(-50%) ;
                z-index: 9999;
            }
    
    </style>
    
    {% if messages %}
        <div class="alert alert-dismissible fade show" role="alert" id="alertMessage">
            {% for message in messages %}
                {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}
                    <div class="alert alert-danger">{{ message }}</div>
                {% elif message.level == DEFAULT_MESSAGE_LEVELS.SUCCESS %}
                    <div class="alert alert-success">{{ message }}</div>
                {% elif message.level == DEFAULT_MESSAGE_LEVELS.WARNING %}
                    <div class="alert alert-warning">{{ message }}</div>
                {% elif message.level == DEFAULT_MESSAGE_LEVELS.INFO %}
                    <div class="alert alert-info">{{ message }}</div>
                {% endif %}
            {% endfor %}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    {% endif %}
     
    

  4. 确保在模板文件中引入Bootstrap的CSS和JS文件,以确保样式和功能正常工作。可以在模板文件中添加以下代码:
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
     
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

britlee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值