0
votes

I am trying to fetch data from a form in Flask the form has a file as well:

app.py

@app.route('/newProjectCreation/', methods=['GET', 'POST'])
def newProjectCreation():
    try:
        if request.method == 'POST':
            # Data Fetch from Models Form
            name = request.form['modelName']
            modelDescription = request.form['modelDescription']
            inputType = request.form.getlist['inputType[]']
            outputType = request.form.getlist['outputType[]']
            model = request.files['model']      # file
            modelLanguage = request.form['modelLanguage']

HTML

It is a simple form (not Flask-WTF form) and a file is being uploaded (here opencv.png is the file uploaded).

Error:

You tried to access the file "model" in the request.files dictionary but it does not exist. The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted. To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names. This was submitted: "opencv.png"
127.0.0.1 - - [11/May/2021 05:24:05] "POST /newProjectCreation HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1926, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1912, in raise_routing_exception
    raise FormDataRoutingRedirect(request)

flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/newProjectCreation) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/newProjectCreation/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.\n\nNote: this exception is only raised in debug mode'

127.0.0.1 - - [11/May/2021 05:24:05] "POST /newProjectCreation HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1926, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1912, in raise_routing_exception
    raise FormDataRoutingRedirect(request)

flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/newProjectCreation) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/newProjectCreation/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.\n\nNote: this exception is only raised in debug mode'

127.0.0.1 - - [11/May/2021 05:24:05] "POST /newProjectCreation/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/debughelpers.py", line 96, in __getitem__
    return oldcls.__getitem__(self, key)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/flaskapp-docker/flaskapp/app.py", line 108, in newProjectCreation
    model = request.files['model']
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/debughelpers.py", line 100, in __getitem__
    raise DebugFilesKeyError(request, key)

flask.debughelpers.DebugFilesKeyError: You tried to access the file "model" in the request.files dictionary but it does not exist. The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted. To fix this error you should provide enctype="multipart/form-data" in your form.

The browser instead transmitted some file names. This was submitted: "opencv.png"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/adiagarwal/fantastic-computing-machine/flaskapp-docker/flaskapp/app.py", line 129, in newProjectCreation
    return {{e}}
TypeError: unhashable type: 'set'

I don't have prior experience in js and I am learning, if possible please simplify the response.

Thank You in advance.

1

1 Answers

1
votes

In your return statement, you are using "{{}}", just remove those and it will work fine. It should be like this

return e