Enctype=Multipart/Form-Data & 无法访问外部请求的配置
Enctype=Multipart/Form-Data & Cannot Access Configuration Outside Request
我正在尝试将文件上传到 Amazon S3,这要求我包含 enctype="multipart/formdata"
。提交表单后,由于包含 enctype,浏览器会用 RuntimeError: cannot access configuration outside request
向我咆哮。我已经提到:RuntimeError: cannot access configuration outside request,但是一旦我配置了 Flask_uploads,我仍然得到相同的 RuntimeError。我不太了解 app.config 部分以及示例中 UploadSet 的参数中包含的内容。关于我做错了什么导致此错误的任何见解都会很棒。谢谢
forms.py
app = Flask(__name__)
# app.config['UPLOADS_DEFAULT_URL'] = 'https://nevcodocs.s3.amazonaws.com/'
app.config['UPLOADED_DOCUMENTS_DEST'] = '/var/uploads'
documents = UploadSet('documents', IMAGES)
configure_uploads(app, documents)
回溯
Traceback (most recent call last):
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\snguyen\Documents\Nevco_Support\nevco-support\nevco_support\main\views.py", line 150, in register
if not form.validate_on_submit():
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_wtf\form.py", line 101, in validate_on_submit
return self.is_submitted() and self.validate()
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\form.py", line 310, in validate
return super(Form, self).validate(extra)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\form.py", line 152, in validate
if not field.validate(self, extra):
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\fields\core.py", line 204, in validate
stop_validation = self._run_validation_chain(form, chain)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\fields\core.py", line 224, in _run_validation_chain
validator(form, self)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_wtf\file.py", line 89, in __call__
if not self.upload_set.file_allowed(field.data, filename):
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_uploads.py", line 370, in file_allowed
return self.extension_allowed(extension(basename))
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_uploads.py", line 380, in extension_allowed
return ((ext in self.config.allow) or
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_uploads.py", line 329, in config
raise RuntimeError("cannot access configuration outside request")
RuntimeError: cannot access configuration outside request
我也试过将配置放在 views.py 而不是 forms.py 中,因为那是我使用 request.files
的地方,但同样的 RunTimeError: cannot access configuration outside request
发生了。也许我不知道的蓝图/app = Flask(__name__)
之间存在冲突?
views.py
blueprint = Blueprint("main", __name__, url_prefix="/")
app = Flask(__name__)
# app.config['UPLOADS_DEFAULT_URL'] = 'https://nevcodocs.s3.amazonaws.com/'
app.config['UPLOADED_DOCUMENTS_DEST'] = '../static/images'
documents = UploadSet('documents', IMAGES)
configure_uploads(app, documents)
@blueprint.route("register/", methods=['GET', 'POST'])
def register():
"""Renders register page."""
form = RegisterForm()
if request.method == 'POST':
if not form.validate_on_submit():
return render_template('main/register.html', page_title="Service Registration",
form=form, form_success=False, message="if not validate_on_submit")
s3 = boto3.resource('s3')
s3.Bucket('nevcodocs/Uploads').put_object(Key='frontview.png', Body=request.files['upload'])
更新:我有一个 settings.py 文件,其中包含 Config、ProdConfig、StagingConfig 等。我认为我遇到的错误与未指向所述文件有关。所以我将 app.config['UPLOADED_DOCUMENTS_DEST']
更改为 app.config.from_pyfile('../settings.py')
。结果是 RuntimeError: no destination for set documents
。参考上传集的文档,第三个参数是 default_dest,但我不确定我设置的是什么?
问题是由于 forms.py 中 FileFields 上的验证器造成的。摆脱验证器解决了这个问题,但我不确定为什么当用户选择文件时这些 FileFields 没有被验证。
我正在尝试将文件上传到 Amazon S3,这要求我包含 enctype="multipart/formdata"
。提交表单后,由于包含 enctype,浏览器会用 RuntimeError: cannot access configuration outside request
向我咆哮。我已经提到:RuntimeError: cannot access configuration outside request,但是一旦我配置了 Flask_uploads,我仍然得到相同的 RuntimeError。我不太了解 app.config 部分以及示例中 UploadSet 的参数中包含的内容。关于我做错了什么导致此错误的任何见解都会很棒。谢谢
forms.py
app = Flask(__name__)
# app.config['UPLOADS_DEFAULT_URL'] = 'https://nevcodocs.s3.amazonaws.com/'
app.config['UPLOADED_DOCUMENTS_DEST'] = '/var/uploads'
documents = UploadSet('documents', IMAGES)
configure_uploads(app, documents)
回溯
Traceback (most recent call last):
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\snguyen\Documents\Nevco_Support\nevco-support\nevco_support\main\views.py", line 150, in register
if not form.validate_on_submit():
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_wtf\form.py", line 101, in validate_on_submit
return self.is_submitted() and self.validate()
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\form.py", line 310, in validate
return super(Form, self).validate(extra)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\form.py", line 152, in validate
if not field.validate(self, extra):
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\fields\core.py", line 204, in validate
stop_validation = self._run_validation_chain(form, chain)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\wtforms\fields\core.py", line 224, in _run_validation_chain
validator(form, self)
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_wtf\file.py", line 89, in __call__
if not self.upload_set.file_allowed(field.data, filename):
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_uploads.py", line 370, in file_allowed
return self.extension_allowed(extension(basename))
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_uploads.py", line 380, in extension_allowed
return ((ext in self.config.allow) or
File "C:\Users\snguyen\Documents\Nevco_Support\venv\lib\site-packages\flask_uploads.py", line 329, in config
raise RuntimeError("cannot access configuration outside request")
RuntimeError: cannot access configuration outside request
我也试过将配置放在 views.py 而不是 forms.py 中,因为那是我使用 request.files
的地方,但同样的 RunTimeError: cannot access configuration outside request
发生了。也许我不知道的蓝图/app = Flask(__name__)
之间存在冲突?
views.py
blueprint = Blueprint("main", __name__, url_prefix="/")
app = Flask(__name__)
# app.config['UPLOADS_DEFAULT_URL'] = 'https://nevcodocs.s3.amazonaws.com/'
app.config['UPLOADED_DOCUMENTS_DEST'] = '../static/images'
documents = UploadSet('documents', IMAGES)
configure_uploads(app, documents)
@blueprint.route("register/", methods=['GET', 'POST'])
def register():
"""Renders register page."""
form = RegisterForm()
if request.method == 'POST':
if not form.validate_on_submit():
return render_template('main/register.html', page_title="Service Registration",
form=form, form_success=False, message="if not validate_on_submit")
s3 = boto3.resource('s3')
s3.Bucket('nevcodocs/Uploads').put_object(Key='frontview.png', Body=request.files['upload'])
更新:我有一个 settings.py 文件,其中包含 Config、ProdConfig、StagingConfig 等。我认为我遇到的错误与未指向所述文件有关。所以我将 app.config['UPLOADED_DOCUMENTS_DEST']
更改为 app.config.from_pyfile('../settings.py')
。结果是 RuntimeError: no destination for set documents
。参考上传集的文档,第三个参数是 default_dest,但我不确定我设置的是什么?
问题是由于 forms.py 中 FileFields 上的验证器造成的。摆脱验证器解决了这个问题,但我不确定为什么当用户选择文件时这些 FileFields 没有被验证。