redirect is not sent to the browser

Asked

Viewed 184 times

0

I made 3 pages on my website: one to register, one to login and another serving as the home page. I need that after the user registers it is sent to the login, that if successful send it to the home page.

I’m using the request e url_for from Flask to this, however, when it runs, it does not change the page url, it just runs the page script (there on app.py). all the logical part on the server works right.

I searched almost all of Stackoverflow (including the gringo), even in Google help, but nothing referring to the same problem as mine. It’s as if the redirect was not sent to the browser, I think because in the tab network of "inspect", the page receives only the JS, CSS, Avast script and itself (html). I get no error on the console, I hit hit to send and the page reloads, even the server claiming that it is all right and that the user is valid.

I thought I’d use the webbrowser, but it opens the link in another tab.

I need to redirect in three locations: from /home to the /register, and of /register to the /login, then, if successful: /login for /home:

app.py/login:

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

    if request.method == 'POST':
        data = request.form.to_dict()
        username = data['User']
        password = hassh.blake2b(data['Password'])

        valid=''
        cur.execute("SELECT * FROM WebApp")
        rows = cur.fetchall()

        a = ''
        for i in username: #adiciona um SALT no hash
            if i.lower() == 'a' or i.lower() == 'e' or i.lower() == 'i' or i.lower() == 'o' or i.lower() == 'u':
                a+=i #coloca as vogais do username na variavel
            else:
                pass
        b = ''.join(reversed(a))
        password += b

        for row in rows:
            dbUsername = str(row[0])
            dbPass = str(row[1])
            if username == dbUsername and password == dbPass:
                valid = True
                break
            else:
                valid = False

        print(str(valid))
        if valid == False: #verificar se o login falhou
            return '<p>Credenciais invalidas</p>', redirect(url_for('login'))

        else: #se não falhou, então deveria enviar o usuario para a home
            return redirect(url_for("home"))
            #return webbrowser.open('http://192.168.0.4:5000/',new=0)

    return render_template('login.html')

home html.:

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8" dir="ltr">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Home</title>
    <link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='home.css') }}">
</head>
<body>    
    <p>aaaa</p>
    <h1>bbbb</h1>



    <script src="{{ url_for('static',filename='home.js') }}"></script>
</body>

app.py/home:

@app.route('/', methods = ['POST','GET'])
def home():
    print('home')
    #return redirect(url_for("login"))
    #flash('gloria')
    return render_template('home.html')

How do I do this redirect(url_for()) really redirect? only worked at home, which has nothing. Alias, when it redirects, it’s like it doesn’t actually send to the server: it executes the def home() and everything in it, but it doesn’t change the page.

Edit1: found that redirect does not work inside if.

  • I tried to create a method in another file, hoping it was some syntax error or something, but it’s still the same thing

  • Where is the home function? I think the problem is not with redirect, replace the redirect Return by "Home" Return, and "Login" Return, so you’ll know if Login actually happened. Because I don’t think it’s coming in "Return redirect(usr_for('home'))

  • it redirects the server to run the def() of the route, but does not redirect the user to the page. I saw that it may be the fault of the Ajax I’m using for front-back communication.

  • I know that the function of home (def) is executed (I used print), but redirect does not

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.