Python Forum

Full Version: Uploading CSV file to flask, only first line being uploaded. Help !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
@upload_csv_blueprint.route('/upload_csv', methods=['GET','POST'])
def upload_file():
    if request.method == 'POST':
        csvfile = request.files['file']
        reader = csv.DictReader(csvfile)
        data = [row for row in reader]
        for row in data:
            date_time_store = row['date_time']
            technician_store = row['technician']
            test_location_store = row['test_location']
            product_serial_number_store = row['product_serial_number']
            test_detail_store = row['test_detail']
            test_result_store = row['test_result']

            query = test_result(date_time = date_time_store,
                                technician_name = technician_store,
                                place_of_test = test_location_store,
                                serial_number=product_serial_number_store,
                                test_details=test_detail_store,
                                result=test_result_store)

            db.session.add(query)
            db.session.commit()
            return('Did it work?')
        else:
            return redirect(url_for('upload_csv.upload_csv_layout'))#
The code above is my current version. This version uploads the second line of the csv file ( the first line being the headings used to match up the columns.)
The csv files i will be uploading could contain up too 1000 lines of data. I was hoping the method I had constructed would loop and add all 1000 entries. But it appears to just quit after the first.

I am constructing my project on a Flask platform, using sql alchemy. If anyone knows where my error lies or knows of an easy way to upload a csv file into a sql database. Would be really appreciated as I have spent the better part of a 2 days searching for the answer.
I think lines#24-26 should be unindented one level. At the moment you will return after first line.
Also you can iterate over reader, no need to construct data list
Im in two minds, half of me wants to give you a hug the other half wants to slap you because im annoyed it took me 2 days and i could not figure out that i just needed to hit backspaces twice :).

Thanks, huge help.

To clarify: unindenting lines 23- 26 by 1 solves the issue.