“Response”对象没有属性“set\u cookie”

2024-04-18 10:49:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我在登录后一直收到这个错误,不知道为什么。请帮忙

@app.post("/login")
def Login(response: Response,credentials: HTTPBasicCredentials = Depends(HTTPBasic())):
    correct_username = secrets.compare_digest(credentials.username, "root")
    correct_password = secrets.compare_digest(credentials.password, "root")
    if (correct_username and correct_password):
        response.status_code = 302
        response.headers["Location"] = "/welcome"
        return response

@app.get("/welcome")
def welcome():
    return {"message": "Hello"}

当我尝试使用/docs登录时,在键入正确的用户名和密码后,我从uvicorn获得服务器错误:

File ".\main.py", line 82, in Login                                                                               
response.headers["Location"] = "/welcome"

AttributeError: 'Response' object has no attribute 'headers'  

Tags: appresponsedef错误usernameloginpasswordheaders