flask icon indicating copy to clipboard operation
flask copied to clipboard

Flaskr tutorial: auth.py raise a type error: tuple indices must be integers not str

Open tim-sheung opened this issue 8 years ago • 3 comments

In the tutorial of flaskr, an error is raised when running the auth.py. In the development server, the following error is raised:

File "~\flaskr\auth.py", line 90, in login
elif not check_password_hash(user['password'], password):
TypeError: tuple indices must be integers or slices, not str

And then I trace back, in line 84, the following code:

user = db.execute(
        'SELECT * FROM user WHERE username = ?', (username,)
).fetchone().

The function fetchone() should return a tuple. But in line 90, the function try to access the tuple by user['password']. Can tuple be accessed by this calling? I'm not pretty sure. But I can access the tuple by calling user[2] instead of user['password']. And actually the same situation in other python scripts. I'm not sure what is the case.

Environment

  • Python version: 3.7.0
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1

tim-sheung avatar Sep 17 '18 15:09 tim-sheung

Be sure to follow the complete tutorial. You didn't set g.db.row_factory = sqlite3.Row. See http://flask.pocoo.org/docs/1.0/tutorial/database/#connect-to-the-database.

def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db

davidism avatar Sep 17 '18 15:09 davidism

I follow it and I directly copy and paste the code. But there's still the problem.

tim-sheung avatar Sep 17 '18 16:09 tim-sheung

Hi I discover the origin of the problem, it seems that my editor is having strange behavior. I solve the problem now using another editor. Sorry for inconvenience caused.

tim-sheung avatar Sep 17 '18 16:09 tim-sheung