from flask import Flask
from forms import LoginForm
app = Flask(__name__)
form = LoginForm()
print(form.username())
出现错误
Traceback (most recent call last):
File “C:/Users/ASUS/flask/helloflask/demos/form/try.py”, line 6, in
form = LoginForm()
File “C:\Users\ASUS\Anaconda3\lib\site-packages\wtforms\form.py”, line 212, in call
return type.call(cls, *args, **kwargs)
File “C:\Users\ASUS\Anaconda3\lib\site-packages\flask_wtf\form.py”, line 88, in init
super(FlaskForm, self).init(formdata=formdata, **kwargs)
File “C:\Users\ASUS\Anaconda3\lib\site-packages\wtforms\form.py”, line 272, in init
super(Form, self).init(self._unbound_fields, meta=meta_obj, prefix=prefix)
File “C:\Users\ASUS\Anaconda3\lib\site-packages\wtforms\form.py”, line 44, in init
translations = self._get_translations()
File “C:\Users\ASUS\Anaconda3\lib\site-packages\wtforms\form.py”, line 85, in _get_translations
return self.meta.get_translations(self)
File “C:\Users\ASUS\Anaconda3\lib\site-packages\flask_wtf\form.py”, line 72, in get_translations
if not current_app.config.get(‘WTF_I18N_ENABLED’, True):
File “C:\Users\ASUS\Anaconda3\lib\site-packages\werkzeug\local.py”, line 347, in getattr
return getattr(self._get_current_object(), name)
File “C:\Users\ASUS\Anaconda3\lib\site-packages\werkzeug\local.py”, line 306, in _get_current_object
return self.__local()
File “C:\Users\ASUS\Anaconda3\lib\site-packages\flask\globals.py”, line 51, in _find_app
raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
from forms import LoginForm
form = LoginForm()
Traceback (most recent call last):
File “”, line 1, in
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\wtforms\form.py”, line 212, in call
return type.call(cls, *args, **kwargs)
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\flask_wtf\form.py”, line 88, in init
super(FlaskForm, self).init(formdata=formdata, **kwargs)
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\wtforms\form.py”, line 278, in init
self.process(formdata, obj, data=data, **kwargs)
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\wtforms\form.py”, line 132, in process
field.process(formdata)
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\wtforms\csrf\core.py”, line 43, in process
self.current_token = self.csrf_impl.generate_csrf_token(self)
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\flask_wtf\csrf.py”, line 134, in generate_csrf_token
token_key=self.meta.csrf_field_name
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\flask_wtf\csrf.py”, line 43, in generate_csrf
if field_name not in session:
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\werkzeug\local.py”, line 379, in contains = lambda x, i: i in x._get_current_object()
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\werkzeug\local.py”, line 306, in _get_current_object
return self.__local()
File “c:\users\asus\flask\helloflask.venv\lib\site-packages\flask\globals.py”, line 37, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
在pycharm中运行了你说的那样的格式也出现了同样的错误,有点懵