So I've been working in Flask and ran into an irritating problem. I'm creating a website and using the redirect function the website doesn't switch pages. Each place I'm attempting to go to can render_template() when not called via redirect. Here's my code (yes, the hashed_password isn't hashed and I should probable redirect to next, but these are are problems for another day):
@app.route('/logIn',methods=['Get', 'Post'])
def logIn():
_name = request.form['inputName']
_password = request.form['inputPassword']
# _hashed_password = bcrypt.hashpw(_password, bcrypt.gensalt( 12 ))
_hashed_password = _password
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute("SELECT * FROM tbl_user WHERE user_name = '" + _name + "'")
user = cursor.fetchone()
print(user[0])
if len(user[2]) is not 0 and user[3] == _password:
active_user = User(user[0])
login_user(active_user)
return (redirect(url_for('home')))
else:
return json.dumps({'message': 'Username or Password not correct'})
@app.route('/Home')
@login_required
def home():
return render_template("home.html")
This all works with a html and js background. The HTML doesn't have any code, only contains the button to be clicked (though if it'll help I can post the html). The JS looks like this for the login button:
$(function() {
$('#btnLogIn').click(function() {
$.ajax({
url: '/logIn',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
Finally, the output spits back the html for the Home page, the one I want to be rendering in the browser (this is the output):
<!DOCTYPE html>
<html lang="en">
<head>
<title>Python Flask Bucket List App</title>
<script src="static/js/jquery-3.3.1.min.js"></script>
<script src="static/js/logout.js"></script>
</head>
<body>
<form class="form-signout">
<button id="btnLogOut" class="btn btn-lg btn-primary btn-block" type="button">Log Out</button>
</form>
</body>
</html>
I've been looking everywhere. Also, adding /Home after the local host domain calls the same GET as the one called after clicking the button and successfully loads the page. If anyone knows how to do this with loading the "next" url, that would be even better.