1

I want to give limitation in row numbers in the below python code, executing code if we have less than 200 rows and nit run the code if it is more than 200. with below code, I am printing the number of rows but the if clause for limiting rows gives me error.

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

ERROR:index:Exception on /CreateStudent[GET]

What i see in the browser: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

@app.route('/CreateStudent', methods=['GET','POST'])
    def upload_student():
        
       if request.method == 'POST':
            csv_file = request.files['file']
            if not csv_file:
                return render_template('error.html')
            csv_file = TextIOWrapper(csv_file, encoding='utf-8')
            csv_reader = csv.reader(csv_file)
            lines= list(csv_reader)
            print(lines)
            if len(lines) < 200:
                for row in lines:
                    if len(row)==4:
                        name=row[0]
                        familyname=row[1]
                        age=row[2]
                        job=row[3]
                        create_student(name,familyname,age,job)
                        time.sleep(2)
                return render_template('success.html')
            return render_template('CreateStudent.html')

when i want to also just print lines i see my result as below: [['Sara','Jacky','22','engineer']] why i have this 2 [[]] in my result, is it because of list?

CodeGirl
  • 81
  • 8
  • Please include the actual error message (e.g. the `Traceback`), otherwise without your original data and/or the context which the code is being executed in, it would be rather difficult for anyone else to see where the problem might be. – metatoaster Jul 12 '20 at 13:49
  • That said, if `csv_reader` is an iterator, calling `list(csv_reader)` would consume every single row thus leaving nothing for the loop inside the `if` statement to consume. – metatoaster Jul 12 '20 at 13:50
  • To add to what @metatoaster said. Change ```lines= len(list(csv_reader)) ``` to ```read_lines = list(csv_reader)```. Then ```lines=len(read_lines)```. Then iterate over read_lines instead of csv_reader in the ```for`` loop. – Adrian Klaver Jul 12 '20 at 15:06
  • @motatoaster: the error code has been adde. – CodeGirl Jul 13 '20 at 05:02
  • @AdrianKlaver: I modified my code and get the errors which i shared. i think it is also similary to the changes you told me to apply. – CodeGirl Jul 13 '20 at 05:06

1 Answers1

1

Here I slightly modified your code and added comments where I've made a modification:

if request.method == 'POST':
    csv_file = request.files['file']
    if not csv_file:
        return render_template('error.html')
    csv_file = TextIOWrapper(csv_file, encoding='utf-8')
    csv_reader = csv.reader(csv_file)
    lines = list(csv_reader)            # <--- read the file into `lines`

    if len(lines) < 200:
        for row in lines:               # <-- we're iterating over `lines` now
            if len(row)==4:
                create_Student(*row)    # <-- no need to extract to variables, simple `*` is enough
        return render_template('success.html')

    return render_template('CreateStudent.html')    # <-- this is returned in case len(lines) >= 200
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • @andrajkesely :Thanks i checked your code but encounter with below error: raise TypeError(TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. I modified my code and i appreciate if you have a look . also i have this error has been occured after modification:" ERROR:index:Exception on /CreateSubscription [GET]" – CodeGirl Jul 13 '20 at 05:07
  • 1
    @CodeGirl I updated my answer. Added `return render_template('CreateStudent.html')` at the end of code. – Andrej Kesely Jul 13 '20 at 06:59
  • 1
    @andejkesely: Many Thanks. It has been successfully done. – CodeGirl Jul 13 '20 at 07:18