Enctype=Multipart/Form-Data & Cannot Access Configuration Outside Request


Enctype=Multipart/Form-Data & Cannot Access Configuration Outside Request



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


enctype="multipart/formdata"


RuntimeError: cannot access configuration outside request



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:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_compat.py", line 33, in reraise
raise value
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_compat.py", line 33, in reraise
raise value
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflaskapp.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UserssnguyenDocumentsNevco_Supportnevco-supportnevco_supportmainviews.py", line 150, in register
if not form.validate_on_submit():
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_wtfform.py", line 101, in validate_on_submit
return self.is_submitted() and self.validate()
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packageswtformsform.py", line 310, in validate
return super(Form, self).validate(extra)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packageswtformsform.py", line 152, in validate
if not field.validate(self, extra):
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packageswtformsfieldscore.py", line 204, in validate
stop_validation = self._run_validation_chain(form, chain)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packageswtformsfieldscore.py", line 224, in _run_validation_chain
validator(form, self)
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_wtffile.py", line 89, in __call__
if not self.upload_set.file_allowed(field.data, filename):
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_uploads.py", line 370, in file_allowed
return self.extension_allowed(extension(basename))
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_uploads.py", line 380, in extension_allowed
return ((ext in self.config.allow) or
File "C:UserssnguyenDocumentsNevco_Supportvenvlibsite-packagesflask_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?


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'])



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?


app.config['UPLOADED_DOCUMENTS_DEST']


app.config.from_pyfile('../settings.py')


RuntimeError: no destination for set documents





Is there anything else I could do to avoid the downvotes? I've gone through the documentation of Flask-Uploads & Flask configuration handling, but I'm still stuck on this problem.
– Steven Nguyen
Jul 2 at 17:35





Why do you have two different apps, one in each file? You should be creating and configuring one app. Your traceback says your view is calling validate_on_submit, but your code doesn't show that. Please edit to include a Minimal, Complete, and Verifiable example.
– davidism
Jul 2 at 19:33



validate_on_submit





I have updated my post (validate_on_submit in views.py). I would like to go with blueprint rather than app = Flask(name). I'm unsure as to how I would configure the blueprint in a similar manner.
– Steven Nguyen
Jul 2 at 20:36



validate_on_submit




1 Answer
1



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages