我正在尝试将文件上传到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,但是我不确定将其设置为什么?
答案 0 :(得分:0)
问题归结于forms.py中FileField上的验证器。摆脱验证器可以解决此问题,但是我不确定为什么用户选择文件时未验证那些FileField。