Flask-Bootstrap Quick_form 认为从 render_template 传入的表单未定义

ele*_*ias 5 python jinja2 flask flask-wtforms

我正在尝试使用 Flask-Bootstrap 渲染 WTF 表单quick_formform但是,即使我将其传递给 ,我也会收到未定义的错误render_template。为什么这不起作用?

File "/home/eron/anaconda/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 190, in template
    {{ form.hidden_tag() }}

File "/home/eron/anaconda/lib/python2.7/site-packages/jinja2/environment.py", line 397, in getattr
    return getattr(obj, attribute)
UndefinedError: 'form' is undefined
Run Code Online (Sandbox Code Playgroud)
from flask.ext.wtf import Form

class NameForm(Form):
    name = StringField('Login', validators=[Required()])
    submit = SubmitField('Submit')

@app.route('/base', methods=['GET','POST'])
def index():
    name = None
    form = NameForm()

    if form.validate_on_submit():
        name = form.name.data
        session['name'] = form.name.data
        return redirect(url_for('base'))

    return render_template('base.html', form=form)
Run Code Online (Sandbox Code Playgroud)
File "/home/eron/anaconda/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/wtf.html", line 190, in template
    {{ form.hidden_tag() }}

File "/home/eron/anaconda/lib/python2.7/site-packages/jinja2/environment.py", line 397, in getattr
    return getattr(obj, attribute)
UndefinedError: 'form' is undefined
Run Code Online (Sandbox Code Playgroud)

Pan*_*los -1

你写道:
class NameForm(Form):
然后
return render_template('base.html', form=form)

首先尝试改变form = formForm = Form

如果这不起作用,请尝试删除form = NameForm()并重试。