我有一个此类用户,该用户根据用户名从sqlite db文件中提取值。我已经仔细检查过,并且我的游标执行检索了正确的元组,但是在User类的实例化上似乎有些错误。
这是我的文件和
User.py
import sqlite3
class User:
# _id becuase id is a reserved keyword
def __init__(self, _id, username, password):
self.id = _id
self.username = username
self.password = password
# added this since self wasn't used and we called User instantiation
@classmethod
def find_by_username(cls, username):
connection = sqlite3.connect("data.db")
cursor = connection.cursor()
query = "SELECT * FROM users WHERE username=?"
# execute has to be in the form of a tuple
result = cursor.execute(query, (username,))
# gets the first result in the set , and if empty returns None
row = result.fetchone()
if row:
# this expands into row[0],row[1],row[2]
user = cls(*row)
else:
user = None
connection.close()
return user
@classmethod
def find_by_id(cls, _id):
connection = sqlite3.connect("data.db")
cursor = connection.cursor()
query = "SELECT * FROM users WHERE id=?"
row = cursor.execute(query, (_id,))
if row:
user = cls(*row)
else:
user = None
connection.close()
return user
Security.py
from werkzeug.security import safe_str_cmp
from user import User
def authenticate(username, password):
user = User.find_by_username(username)
if user and safe_str_cmp(user.password, password):
return user
def identity(payload):
user_id = payload["identity"]
return User.find_by_id(user_id)
我已验证User.py中的数据库调用返回正确的元组(id,“用户名”,“密码”)。这是我得到的堆栈跟踪:
Traceback (most recent call last):
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_jwt/__init__.py", line 176, in decorator
_jwt_required(realm or current_app.config['JWT_DEFAULT_REALM'])
File "not_importtant_path/venv/lib/python3.6/site-packages/flask_jwt/__init__.py", line 162, in _jwt_required
_request_ctx_stack.top.current_identity = identity = _jwt.identity_callback(payload)
File "not_importtant_path/code/security.py", line 13, in identity
return User.find_by_id(user_id)
File "not_importtant_path/code/user.py", line 40, in find_by_id
user = cls(*row)
TypeError: __init__() missing 2 required positional arguments: 'username' and 'password'
答案 0 :(得分:2)
您需要在fetchone
类方法的cursor.execute()
之后find_by_id
。您已经在find_by_username
类方法中正确地做到了这一点。