首页 新闻 会员 周边 捐助

明明定义了form,但是运行代码告诉我未定义?

0
悬赏园豆:5 [已关闭问题] 关闭于 2019-05-23 10:10

我的python代码:
class LoginForm(FlaskForm):
username = StringField('用户名')
password = PasswordField('密码')
password2 = PasswordField('确认密码')
submit = SubmitField('提交')

@app.route('/form', methods=['GET', 'POST'])
def login():
login_form = LoginForm
return render_template('index2.html', login_form=login_form)

我的html代码:

<form method="post">
{{login_form.username.label}}{{login_form.username}}
</form>

报错信息:
127.0.0.1 - - [23/May/2019 09:40:20] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "F:\flask_py\lib\site-packages\flask\app.py", line 2309, in call
return self.wsgi_app(environ, start_response)
File "F:\flask_py\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "F:\flask_py\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\flask_py\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "F:\flask_py\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "F:\flask_py\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "F:\flask_py\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "F:\flask_py\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "F:\flask_py\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "F:\flask_py\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functionsrule.endpoint
File "F:\Flask_text_demo\flask_py\Falsk_WTF_demo3.py", line 63, in index
return render_template('index2.html')
File "F:\flask_py\lib\site-packages\flask\templating.py", line 135, in render_template
context, ctx.app)
File "F:\flask_py\lib\site-packages\flask\templating.py", line 117, in _render
rv = template.render(context)
File "F:\flask_py\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "F:\flask_py\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "F:\flask_py\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\flask_py\lib\site-packages\jinja2_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "F:\Flask_text_demo\flask_py\templates\index2.html", line 24, in top-level template code
{{login_form.username.label}}{{login_form.username}}
File "F:\flask_py\lib\site-packages\jinja2\environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'login_form' is undefined
127.0.0.1 - - [23/May/2019 09:40:20] "GET /?debugger=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2019 09:40:20] "GET /?debugger=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2019 09:40:20] "GET /?debugger=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2019 09:40:20] "GET /?debugger=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2019 09:40:20] "GET /?debugger=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2019 09:40:20] "GET /?debugger=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [23/May/2019 09:40:20] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "F:\flask_py\lib\site-packages\flask\app.py", line 2309, in call
return self.wsgi_app(environ, start_response)
File "F:\flask_py\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "F:\flask_py\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\flask_py\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "F:\flask_py\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "F:\flask_py\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "F:\flask_py\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "F:\flask_py\lib\site-packages\flask_compat.py", line 35, in reraise
raise value
File "F:\flask_py\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "F:\flask_py\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functionsrule.endpoint
File "F:\Flask_text_demo\flask_py\Falsk_WTF_demo3.py", line 63, in index
return render_template('index2.html')
File "F:\flask_py\lib\site-packages\flask\templating.py", line 135, in render_template
context, ctx.app)
File "F:\flask_py\lib\site-packages\flask\templating.py", line 117, in _render
rv = template.render(context)
File "F:\flask_py\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "F:\flask_py\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "F:\flask_py\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "F:\flask_py\lib\site-packages\jinja2_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "F:\Flask_text_demo\flask_py\templates\index2.html", line 24, in top-level template code
{{login_form.username.label}}{{login_form.username}}
File "F:\flask_py\lib\site-packages\jinja2\environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'login_form' is undefined

阿文-awen的博客的主页 阿文-awen的博客 | 初学一级 | 园豆:191
提问于:2019-05-23 10:07
< >
分享
所有回答(1)
0

阿文-awen的博客 | 园豆:191 (初学一级) | 2019-05-23 10:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册