【发布时间】:2020-05-22 06:30:36
【问题描述】:
我正在使用 flask-bootstrap 将 Bootstrap 前端用于烧瓶 Web 应用程序。不幸的是,由于我已经开始使用flask-bootstrap 和flask-nav,我无法显示Flash 消息。
这是base.html:
{% extends 'bootstrap/base.html' %}
{% block navbar %}
{{ nav.mynavbar.render() }}
{% endblock %}
<html>
<body>
<hr>
<div class='container'>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endwith %}
{% block content %}{% endblock %}
</div>
</body>
</html>
这是保存更改时应闪烁消息的视图之一:
@app.route('/model/<model_name>/edit', methods=['GET', 'POST'])
def edit(model_name):
"""Edit model's configuration.
Args:
model_name [str]: The name of the model we want to change the
configuration.
"""
# return to index page if model is not found in the database
model = DSModel.query.filter_by(name=model_name).first()
if not model:
flash('Model {} not found.'.format(model_name))
return redirect(url_for('index'))
# pre-load current model's configuration
form = ConfigurationForm()
# if it's a POST request, it means the user is trying to change the model's
# configuration. Save changes in the database
if request.method == 'POST': # and form.validate_on_submit():
model.configuration.configuration = form.value.data
model.configuration.datetime = datetime.datetime.utcnow()
db.session.add(model)
db.session.commit()
flash('Your changes have been saved.', 'success')
return redirect(url_for('edit', model_name=model_name))
else:
form.value.data = model.configuration.configuration
return render_template('edit.html', form=form)
最后,这是__init__.py:
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import Navbar, View
from flask_sqlalchemy import SQLAlchemy
nav = Nav()
@nav.navigation()
def mynavbar():
return Navbar(
'Dashboard',
View('Home', 'index'),
View('Login', 'login')
)
app = Flask(__name__)
Bootstrap(app)
nav.init_app(app)
app.config.from_object('config')
db = SQLAlchemy(app)
from app import views, models
我认为我的base.html 文件一定有什么奇怪的地方,但我对 HTML 不是很熟悉,所以我找不到问题所在。我已经查看了在线示例(即here),但格式似乎与我正在做的非常相似。
编辑:这是edit.html 文件:
{% extends 'base.html' %}
{% block content %}
<h1>Update configuration</h1>
<form action='' method='post' name='configuration'>
<p>
Please update the current model configuration:<br>
{{ form.value(cols='150', rows='20') }}
<p><input type='submit' value='Save'></p>
</form>
{% endblock %}
【问题讨论】:
-
<div class=container>...</div>所有的东西都应该在content块内。 -
嗯,我不确定我是否理解您的建议。我希望在
base.html模板中只用with块编写所有内容。这样我就可以在其他模板中重复使用它,只需扩展base.html。 -
我不是说添加额外的东西。只需将正文之间的所有内容移动到
{%block content%}things goes here{%endblock%} -
我明白了!对困惑感到抱歉。即使这样做了,不幸的是消息没有显示出来。
-
您能展示一下您的其他模板 (
edit.html) 吗?
标签: python html twitter-bootstrap flask flash-message