I'm trying to upload files to Amazon S3 which required me to include enctype="multipart/formdata"
. Once I submit the form, the browser barks at me with RuntimeError: cannot access configuration outside request
due to the inclusion of enctype. I have referred to: RuntimeError: cannot access configuration outside request, but I am still getting the same RuntimeError once I have configured Flask_uploads. I don't quite understand the app.config part as well as what to include in the parameters of the UploadSet in the example. Any insight as to what I'm doing wrong to cause this error would be great. Thanks
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
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
I've also tried putting the configuration in views.py instead of forms.py as that's where I'm using request.files
, but the same RunTimeError: cannot access configuration outside request
occurs. Perhaps there'a conflict between the blueprint/app = Flask(__name__)
that I'm not aware of?
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'])
Update: I have a settings.py file which contains Config, ProdConfig, StagingConfig, etc. I believe the error I'm getting relates to not pointing to said file. So I changed the app.config['UPLOADED_DOCUMENTS_DEST']
to app.config.from_pyfile('../settings.py')
. That results in RuntimeError: no destination for set documents
. Referencing the doc for Upload Sets, the 3rd parameter is default_dest, but I'm unsure as to what I set that to?
Issue was due to the validators on the FileFields in forms.py. Getting rid of the validators fixed the problem, but I'm unsure as to why those FileFields aren't validated when the user selects a file.