Flask-TypeError:视图函数未返回有效响应

时间:2019-05-28 09:20:59

标签: python flask

当我运行服务器时,出现TypeError问题。 这是我得到的错误: TypeError:视图函数未返回有效响应。该函数返回None或不返回return语句结束。

@app.route('/add', methods=['GET','POST'])
def add_user():
        try:
            _idno = request.form['idno']
            _name = request.form['name']
            _date = request.form['date']
            _totalamount =request.form['totalamount']

            # validate the received values
            if _idno and _name and _date and _totalamount and request.method == 'POST':
                # do not save password as a plain text
                # save edits
                sql = "INSERT INTO tbl_flask(idno,name,date,totalamount)VALUES(%s, %s, %s,%s)"
                data = (_idno, _name, _date, _totalamount)
                conn = mysql.connect()
                cursor = conn.cursor()
                cursor.execute(sql, data)
                conn.commit()
                flash('User added successfully!')
                return redirect('/')
            else:
                return 'Error While Adding User'
        except Exception as e:
            print(e)

@app.route('/')
def users():
    try:
        conn = mysql.connect()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        cursor.execute("SELECT * FROM tbl_flask ")
        rows = cursor.fetchall()
        table=Results(rows)
        table.border=True
        return render_template('users.html',table=table)
    except Exception as e:
        print(e)

1 个答案:

答案 0 :(得分:0)

除了必须有return外,在每种情况下都必须返回一些内容。 您甚至可以像这样return (e)