'Response'对象没有属性'set_cookie'。

问题描述 投票:0回答:1

我一直得到这个错误所有的时间后,我登录,不知道为什么。请大家帮忙

@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'  
python fastapi
1个回答
0
投票

你检查过导入了吗?应该是 from fastapi import Response 我在我的代码中也有类似的代码片段,它与这个导入和当前的fastapi版本一起工作。

© www.soinside.com 2019 - 2024. All rights reserved.