I'm currently working on an application using flask and python. At the moment I'm trying to get a flash message to display when the user is redirected to the login page after successfully submitting the registration form but I can't get the message to display.
I've tried reading the documentation, googling and asking my AI buddy but no fixes seem the resolve the issue.
Im still quite new to programming so please bear that in mind!
Thanks in advance,
- I have generated and implemented a secret-key.
- I have all the necessary functions and libraries implemented.
- All of the html files are in the same templates file.
- The line is not being skipped (it prints the statement above successfully)
Besides not flashing the message everything works just fine and all the data is stored and the user is redirected back to the login page. My AI buddy told me to inspect that page to see if I could find the flash message there, and I couldn't. Don't know if that provide any clues.
@app.route("/register", methods=["GET", "POST"]) def register(): """Register user"""
# Fetching the information form the HTML form
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirm_password = request.form.get("confirm_password")
# Checking db ensuring username is free
existing_user = db.execute("SELECT COUNT(*) FROM users WHERE username = :username", username=username)
if existing_user[0]['COUNT(*)'] > 0:
return apology("Username already taken")
# Ensuring that form is filled out properly
if not username or not password or not confirm_password:
return apology("please fill out all fields")
elif password != confirm_password:
return apology("Passwords do not match")
# Hashing password, storing hashkey and username in db, redirect user to login with "success-message"
else:
hashed_password = generate_password_hash(password)
db.execute("INSERT INTO users (username, hash) VALUES(?, ?)", username, hashed_password)
# Retriving the user_id we just inserted from the database
user = db.execute("SELECT id FROM users WHERE username = :username", username = username)
user_id = user[0]["id"]
session["user_id"] = user_id
# Ensuring line flash line is not skipped
print("Flash message is about to be set")
flash("You have successfully registered and are logged in!", "success")
return redirect("/account")
return render_template("register.html")
And the code snippet from the HTML file layout.html:
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="container">
<div class="alert alert-success alert-dismissible fade show mt-3" role="alert">
<ul class="mb-0">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
{% endwith %}
Please don't include code as images. You make it difficult for people trying to respond to you to reproduce your code (nobody has time to start typing your code out).
In your code, you're flashing messages with categories (see documentation). You should thus retrieve them (on the client side) with categories too which in turn means that each member of the returned message is now a tuple. Based on this, change your code (on the html page) to
/login. You're checking for the flashed message onlayout.html. You should make sure that/loginactually renderslayout.html