首页 > 解决方案 > 烧瓶重定向发送获取请求但不重定向

问题描述

我正在使用 Flask 创建一个用户身份验证系统,作为其中的一部分,我必须将用户重定向到其他一些端点。但是,每当该行代码运行时,我都可以看到正在发送 GET 请求,尽管我的页面没有重定向。

我有一个 html 表单,用户可以在其中输入他们的数据。我通过 javascript 将数据发送到我的烧瓶后端,如下所示:

let loginForm = document.querySelector('form'); 



 loginForm.addEventListener('submit', (event) =>{
 //....
fetch(API_URL, {
    method: 'POST',
    body: JSON.stringify(loginDatatoSend),
    headers:{
        'content-type': 'application/json'
    }
})
.then(response => response.json())
    .then(loginResponse =>{
          //....
    });

    }); 

我将其发送到 /login 的位置

在烧瓶中,我的 /login 路线如下:

@auth.route('/login', methods=['GET', 'POST'])
def login():

if request.method == 'POST':
    #....
    if account_exists: 
        result = login_user(email) 
        print("LOGIN USER", result) 

        return redirect(url_for('views.home'))
      

    else:
        response_message = {
            "code": 0, 
            "message": "Account doesn't exist" 
        }
        return json.dumps(response_message) 

   
return render_template('login.html')

这是在文件 auth.py 中,因为我正在使用蓝图;我正在views.py 中访问home (/) 路由。当我的代码到达该行时,我可以看到一个 GET 请求被发送到 /,状态代码为 200。另外请注意,当我将重定向行直接移动到 def login() 下时,我能够成功重定向。

我的views.py的/路由很简单:

@views.route('/', methods=['GET']) 
def home():
    return render_template('home.html')

谢谢你的帮助。

标签: pythonflaskredirect

解决方案


推荐阅读