1

I'm creating a login, register account website and it works well except for when i use the render template command as it loads the page successfully but then the url of the page is the URL of the previous page and when I refresh it, it goes back to the previous page. But when I use the redirect(url_for command I'm not able to pass variables through it

class LoginForm(FlaskForm):
    username= StringField(validators=[InputRequired(),Length(min=4,max=20)],render_kw={"placeholder":"Username"})
    password= PasswordField(validators=[InputRequired(),Length(min=8,max=20)],render_kw={"placeholder":"Password"})
    submit =SubmitField("Login")

@app.route("/login",methods= ['GET','POST'])
def login():
    form=LoginForm()
    print("in login html")
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user:
            if user.password==form.password.data:
                login_user(user)
                print("password entered")
                userUsername=form.username.data
                #return redirect(url_for('dashboard',userUser=userUsername,))
                return render_template("dashboard.html",userUser=userUsername)
            else:
                print("incorrect pass")
            
    return render_template("login.html",form=form)

@app.route("/dashboard",methods= ['GET','POST'])
@login_required
def dashboard():
    return render_template("dashboard.html")

this is my python code, my dashboard.html-

<!DOCTYPE html>
<html lang="en">

<head>

    <title>Dashboard</title>
    <link href = "../static/main.css" rel = "stylesheet">    

</head>
<div class = "container1">
<body>
    <div data-role="page" id="welcome" data-url="{{ url_for('dashboard') }}">

    {%extends "layout.html"%}
    {%block content%}
    <p>{{userUser}}</p>
    <h1>Hello you are logged in. </h1>
    <div class = "container1">Welcome {{userUser}}!</div>
    <form action ="{{url_for('login')}}" method = POST>
        <br> <button type = "submit">Logout</button>
        </form>
    </div>
    {%endblock%}
    </body>

</html>

now my login.html

<!DOCTYPE html>
<html lang = "en">
<head>
    <link href = "../static/main.css" rel = "stylesheet">   
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title> 
</head>    
<body>
<div class = "container">

<h1>Login</h1>
<form method="POST" actions="">
{{  form.hidden_tag()  }}
{{  form.username  }}
{{  form.password  }}
{{  form.submit  }}

</form>
<a href="{{ url_for('register') }}">Don't have an account? Sign Up</a>
<div class = "message"> {{errorMessage  | safe}}</div>
</div>
</body>
</html>

when i pass userUser through redirect(url_for it doesnt work but with render templates it does but the url remains /login

  • Does this answer your question? [redirect while passing arguments](https://stackoverflow.com/questions/17057191/redirect-while-passing-arguments) – Zenek Aug 29 '22 at 15:06
  • nope, ive tried that but still doesnt work. redirect doesnt transfer my variables to the html page – flawlessassassin Aug 29 '22 at 15:14

0 Answers0