Flash 消息未采用 bootstrap 样式
Flash messages not picking up bootstrap styling
首先感谢您的帮助。
我创建了一个小型 flask 网络应用程序来跟踪“待办事项”项目。作为应用程序的一部分,我添加了一些 flash 消息来帮助登录、注册等。我似乎将信息正确地提供给了浏览器,但是 bootstrap [=26= 的样式] 不只显示纯文本消息。
这是 flask 应用程序的基础 html。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='ToDoList.css')}}" />
</head>
<body>
<header class="site-header">
<nav class="navbar navbar-expand-sm navbar-dark bg-steel fixed-top">
<div class="container">
<a class="navbar-brand mr-4" href="/">Just Do It Already</a>
<div class="navbar-nav ">
<a class="nav-item nav-link" href="{{ url_for('profile') }}">Profile</a>
{% if current_user.is_authenticated %}
<a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a>
<a class="nav-item nav-link" href="{{ url_for('new_post') }}">New Task</a>
{% else %}
<a class="nav-item nav-link" href="{{ url_for('log_in') }}">Login</a>
<a class="nav-item nav-link" href="{{ url_for('register') }}">Register</a>
{% endif %}
</div>
</div>
</div>
</div>
</nav>
</header>
<main role="main" class="container">
<div class='row'>
<div class="col-md-8 mx-auto">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% block body %}
{% endblock %}
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
</html>
我的 flask 消息都包含 chrome 开发工具检查中显示的类别,只是消息没有样式。
知道我错过了什么吗?
bootstrap中没有alert-error
class,需要用alert-danger
或alert-warning
。你可以看到所有选项 here
<div class="alert alert-primary" role="alert">
A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
A simple info alert—check it out!
</div>
<div class="alert alert-light" role="alert">
A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert—check it out!
</div>
所以你的类别应该是danger
或warning
,例如:
flash('Login failed etc etc', 'danger')
首先感谢您的帮助。
我创建了一个小型 flask 网络应用程序来跟踪“待办事项”项目。作为应用程序的一部分,我添加了一些 flash 消息来帮助登录、注册等。我似乎将信息正确地提供给了浏览器,但是 bootstrap [=26= 的样式] 不只显示纯文本消息。
这是 flask 应用程序的基础 html。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='ToDoList.css')}}" />
</head>
<body>
<header class="site-header">
<nav class="navbar navbar-expand-sm navbar-dark bg-steel fixed-top">
<div class="container">
<a class="navbar-brand mr-4" href="/">Just Do It Already</a>
<div class="navbar-nav ">
<a class="nav-item nav-link" href="{{ url_for('profile') }}">Profile</a>
{% if current_user.is_authenticated %}
<a class="nav-item nav-link" href="{{ url_for('logout') }}">Logout</a>
<a class="nav-item nav-link" href="{{ url_for('new_post') }}">New Task</a>
{% else %}
<a class="nav-item nav-link" href="{{ url_for('log_in') }}">Login</a>
<a class="nav-item nav-link" href="{{ url_for('register') }}">Register</a>
{% endif %}
</div>
</div>
</div>
</div>
</nav>
</header>
<main role="main" class="container">
<div class='row'>
<div class="col-md-8 mx-auto">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% block body %}
{% endblock %}
</div>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
</html>
我的 flask 消息都包含 chrome 开发工具检查中显示的类别,只是消息没有样式。
知道我错过了什么吗?
bootstrap中没有alert-error
class,需要用alert-danger
或alert-warning
。你可以看到所有选项 here
<div class="alert alert-primary" role="alert">
A simple primary alert—check it out!
</div>
<div class="alert alert-secondary" role="alert">
A simple secondary alert—check it out!
</div>
<div class="alert alert-success" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-danger" role="alert">
A simple danger alert—check it out!
</div>
<div class="alert alert-warning" role="alert">
A simple warning alert—check it out!
</div>
<div class="alert alert-info" role="alert">
A simple info alert—check it out!
</div>
<div class="alert alert-light" role="alert">
A simple light alert—check it out!
</div>
<div class="alert alert-dark" role="alert">
A simple dark alert—check it out!
</div>
所以你的类别应该是danger
或warning
,例如:
flash('Login failed etc etc', 'danger')