我正在尝试将文件上传到要求我包含enctype="multipart/formdata"
. 一旦我提交了表单,RuntimeError: cannot access configuration outside request
由于包含了 enctype,浏览器就会对我咆哮。我已经提到:RuntimeError: cannot access configuration outside request,但是一旦我配置了 Flask_uploads,我仍然会得到相同的 RuntimeError。我不太了解 app.config 部分以及示例中 UploadSet 的参数中包含的内容。关于我做错了什么导致此错误的任何见解都会很棒。谢谢
表格.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__)
也许我不知道的蓝图/之间存在冲突?
视图.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,但我不确定我将其设置为什么?