Home > Back-end >  Flask file upload form on the site page error where is my mistake
Flask file upload form on the site page error where is my mistake

Time:04-12

Have a good day!

I'm trying to use the file upload form in the second form on the site page.

I made a route for processing the file, but further (print ('upload complete 2')) the movement does not go. The process stops at - print('upload complete 2').

What can be done to make it work?

It's Flask route with upload code

@app.route('/upload2', methods=['POST'])
def upload2():  # put application's code here
    if request.method=="POST":
        print('upload complete 1')
        upload_excel=request.files['upload_excel_2']
        print('upload complete 2')
        print(upload_excel.filename)
        if upload_excel.filename != '':
            print('upload complete 3')
            filepath=os.path.join(app.config["UPLOAD_FOLDER"],upload_excel.filename)
            upload_excel.save(filepath)
            data=pd.read_excel(upload_excel)
            data.to_sql('rashodi', con=db.engine, if_exists="append", index=False)
            return print(data)
    return print('upload complete')

It's html page upload code

<div >
        <div >
            <form  method="POST" action="/upload2" enctype="multipart/form-data">
                <label for="form_input" >
                    <input type="file" id="form_input_2"  name="upload_excel_2">
                    <span >Change file</span>
                </label>
                <div >
                    <button type="submit" name="upload_2" >Upload File</button>
                </div>
            </form>
        </div>

It's terminal debug

[2022-04-11 22:36:12,738] ERROR in app: Exception on /upload2 [POST]
Traceback (most recent call last):
  File "C:\Appforyou\infovenv\lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Appforyou\infovenv\lib\site-packages\flask\app.py", line 1519, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Appforyou\infovenv\lib\site-packages\flask\app.py", line 1538, in finalize_request
    response = self.make_response(rv)
  File "C:\Appforyou\infovenv\lib\site-packages\flask\app.py", line 1701, in make_response
    raise TypeError(
TypeError: The view function for 'upload2' did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [11/Apr/2022 22:36:12] "POST /upload2 HTTP/1.1" 500 -
upload complete 1
upload complete 2

upload complete

CodePudding user response:

print does return None so

return print('upload complete')

does returns None, please just return string you want i.e.

return 'upload complete'

other return print uses data which is not guaranteed to be string therefore, please convert it accordingly that is replace

return print(data)

using

return str(data)

CodePudding user response:

@app.route('/', methods=['GET','POST'])
@app.route('/index')
def home_page():  # put application's code here
    if request.method=="POST":
        if request.files['upload_excel'] != '':
            upload_excel=request.files['upload_excel']
            if upload_excel.filename != '':
                filepath=os.path.join(app.config["UPLOAD_FOLDER"],upload_excel.filename)
                upload_excel.save(filepath)
                data=pd.read_excel(upload_excel)
                data.to_sql('kotel', con=db.engine, if_exists="append", index=False)
                return print(data)
        elif request.files['upload_excel_2'] != '':
            upload_excel=request.files['upload_excel_2']
            if upload_excel.filename != '':
                filepath=os.path.join(app.config["UPLOAD_FOLDER"],upload_excel.filename)
                upload_excel.save(filepath)
                data=pd.read_excel(upload_excel)
                data.to_sql('rashodi', con=db.engine, if_exists="append", index=False)
                return print(data)
    return render_template('index new.html')
  • Related