我试图重新创建随flask-bootstrap提供的示例应用程序,但是遇到了渲染导航栏的问题。我收到错误: inja2.exceptions.UndefinedError:' flask_nav.Nav object'没有属性' fronend_top'
示例应用程序位于: https://github.com/mbr/flask-bootstrap/tree/master/sample_app
这里的flask-nav示例如下: 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 %}
感谢您的帮助!
答案 0 :(得分: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)
答案 1 :(得分:0)
您要使用flask-boostrap4
尝试此操作吗?如果是这样,我发现它的Navbar Renderer损坏了,并导致了您发现的相同错误。将flask-boostrap
v3与BS 3一起使用似乎可以解决此问题。