创建表单类后执行 form.username() 报错 RuntimeError: Working outside of application context

表单

#1
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.

想请教一下这样的问题怎么解决。

或者说,类似书上这样的代码,应该在什么样的环境下运行。


#2

你的问题没必要使用图片上传,请使用代码块格式化

插入代码块的方式:除了将所有代码缩进四格,更方便的插入代码块方式是使用三个连续的反引号作为开始和结束标记,比如:

```
def hello():
    return ‘Hello World’
```

会被渲染为:

 def hello():
     return 'Hello World'

具体请看置顶帖子 技术提问帖发帖规则(创建提问帖前必读)


#3

书中是在flask shell命令行界面中执行的,不是纯粹的执行python进入命令行,而flask shell会自动进入Flask应用上下文环境。

如果你要在代码中执行,要这样写:

app = Flask(__name__)

with app.app_context():
    form = LoginForm()
   ...

(不能复制你的代码有点不爽)


#4

哈哈哈,抱歉,下次明白了。
我在flask shell中运行出现了这样的错误

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中运行了你说的那样的格式也出现了同样的错误,有点懵


#5
RuntimeError: Working outside of request context.

这个错误说明还需要请求上下文。具体知识点看书本《2.4.2 激活上下文》小节。

$ flask shell
>>> from forms import LoginForm
>>> with app.test_request_context():
>>>     form = LoginForm()
>>>

#6

问题解决了,谢谢。


#7

书上那一段第一句话有提到,这是单纯使用 WTForms 创建的表单类(继承 wtfoms.Form 基类),所以不依赖程请求上下文。而你创建的表单类继承了 Flask-WTF 提供的 FlaskForm 基类,它内部某些方法依赖于请求上下文。

你截图的这一段代码需要紧接着 106 页中部的代码执行。


#8

明白了,谢谢。