【发布时间】:2021-07-15 00:18:28
【问题描述】:
File ".\core\users\login.py", line 22, in login_user
db_user = crud.get_Login(
File ".\api\crud.py", line 39, in get_Login
db_user.password.encode('utf-8'))
AttributeError: 'bytes' object has no attribute 'encode'
这个错误与 Python 中的 Base64 相关
这是我的core\users\login.py:
@router.post("/login")
def login_user(user: schemas.UserLogin, db: Session = Depends(get_db)):
db_user = crud.get_Login(
db, username=user.username, password=user.password)
if db_user == False:
raise HTTPException(status_code=400, detail="Wrong username/password")
return {"message": "User found"}
和api\crud.py:
def get_Login(db: Session, username: str, password: str):
db_user = db.query(models.UserInfo).filter(
models.UserInfo.username == username).first()
print(username, password)
pwd = bcrypt.checkpw(password.encode('utf-8'),
db_user.password.encode('utf-8'))
return pwd
我尝试了这个解决方案,但没有任何效果 AttributeError: 'bytes' object has no attribute 'encode'; base64 encode a pdf file
【问题讨论】:
-
我知道我
decode字节和encode字符串,但我尝试了多种解决方案,但没有任何用处 -
您展示的代码中没有任何内容与 Base64 有任何关系,我不明白您为什么会期望它与此处相关。
-
"我知道我
decode字节和encode字符串" 好吧,当您尝试考虑 a) 在每种情况下您有哪些; b) 在每种情况下,您想要中的哪一个?如果你有一个bytes并想要一个bytes,你应该怎么做才能转换它?
标签: python encoding crud bcrypt fastapi