首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用flask-bootstrap和flask-nav的Flash消息

使用flask-bootstrap和flask-nav的Flash消息
EN

Stack Overflow用户
提问于 2017-03-02 22:45:26
回答 1查看 1.4K关注 0票数 0

我正在使用flask-bootstrap为flask web应用程序使用Bootstrap前端。不幸的是,由于我已经开始使用flask-bootstrapflask-nav,我无法显示闪光消息。

这是base.html

代码语言:javascript
复制
{% 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>

这是保存更改时应闪现消息的视图之一:

代码语言:javascript
复制
@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

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
{% 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 %}
EN

回答 1

Stack Overflow用户

发布于 2017-03-03 10:18:57

尝试将您的base.html编辑为以下内容:

代码语言:javascript
复制
{% 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 %}
      {% endwith %}
     </div>
      {% block content %}{% endblock %}
      </div>
  </body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42558451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档