0

我正在尝试重新创建flask-bootstrap 提供的这个示例应用程序,但在呈现导航栏时遇到了问题。我收到错误:inja2.exceptions.UndefinedError: 'flask_nav.Nav object' has no attribute 'fronend_top'

示例应用程序在这里: https ://github.com/mbr/flask-bootstrap/tree/master/sample_app

烧瓶导航示例在这里: http: //pythonhosted.org/flask-nav/getting-started.html#rendering-the-navbar

据我所知,我正在做的事情似乎是正确的,但我猜导航没有被传递给网页上下文?没有把握。

这是我的 server.py

from flask import Blueprint, render_template, flash, redirect, url_for
from flask_bootstrap import __version__ as FLASK_BOOTSTRAP_VERSION
from flask_bootstrap import Bootstrap
from flask_nav.elements import Navbar, View, Subgroup, Link, Text, Separator
from markupsafe import escape
from flask_nav import Nav
from forms import SignupForm
from flask import Flask
#from nav import nav


frontend = Blueprint('frontend', __name__)

# We're adding a navbar as well through flask-navbar. In our example, the
# navbar has an usual amount of Link-Elements, more commonly you will have a
# lot more View instances.
nav = Nav()
nav.register_element('frontend_top', Navbar(
    View('Flask-Bootstrap', '.index'),
    View('Home', '.index'),
    View('Forms Example', '.example_form'),
    View('Debug-Info', 'debug.debug_root'), ))


# Our index-page just shows a quick explanation. Check out the template
# "templates/index.html" documentation for more details.
@frontend.route('/')
def index():
    return render_template('index.html', nav=nav)



app = Flask(__name__)

app.register_blueprint(frontend)
bootstrap = Bootstrap(app)
nav.init_app(app)

app.run(debug=True)

这是在我的 base.html 中引发错误的行

{% block navbar %}
{{nav.fronend_top.render()}}
{% endblock %}

谢谢你的帮助!

4

2 回答 2

0

你在尝试这个flask-boostrap4吗?如果是这样,我发现它的导航栏渲染器已损坏并导致您发现相同的错误。使用flask-boostrapv3 和 BS 3 似乎可以解决它。

于 2018-12-21T05:05:43.177 回答
0

我能够使用这个: https : //github.com/mbr/flask-nav/blob/master/example/init .py

这: http: //pythonhosted.org/flask-nav/getting-started.html#rendering-the-navbar

创建一个有效的简化版本。我从 init 示例中的 create_app 方法开始,该方法有效,然后在下面的示例中删除了该方法。也许它与它是一个蓝图有关?所以它没有导航对象?

from flask import Blueprint, render_template, flash, redirect, url_for
from flask_bootstrap import __version__ as FLASK_BOOTSTRAP_VERSION
from flask_bootstrap import Bootstrap
from flask_nav.elements import Navbar, View, Subgroup, Link, Text, Separator
from markupsafe import escape
from flask_nav import Nav
from forms import SignupForm
from flask import Flask
#from nav import nav
app = Flask(__name__)

nav = Nav()
topbar = Navbar('',
    View('Home', 'index'),
)
nav.register_element('top', topbar)


# not good style, but like to keep our examples short
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/products/<product>/')
def products(product):
    return render_template('index.html', msg='Buy our {}'.format(product))

@app.route('/about-us/')
def about():
    return render_template('index.html')


# Shows a long signup form, demonstrating form rendering.
@app.route('/example-form/', methods=('GET', 'POST'))
def example_form():
    form = SignupForm()

    if form.validate_on_submit():
        flash('Hello, {}. You have successfully signed up'
              .format(escape(form.name.data)))

        # In a real application, you may wish to avoid this tedious redirect.
        return redirect(url_for('.index'))

    return render_template('signup.html', form=form)


nav.init_app(app)
bootstrap = Bootstrap(app)
app.run(debug=True)
于 2018-03-28T17:34:53.437 回答