Whenever I first load into the website after running flask, I get the '404 not found' error and I am not sure how to change it to just show the login page.
And when I try to register a new user, the information does get recorded and hashed in the database but I receive a 500 internal server error where I can only access the homepage if I used the login page. I am not sure how I should go about fixing this so would greatly appreciate any help on this.
application.py(login)
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 403)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 403)
# Query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to portfolio page
return redirect("/portfolio")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
application.py (register)
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 403)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 403)
# Ensure confirm password is submitted
elif not request.form.get("confirm-password"):
return apology("must confirm password", 403)
# if confirm password does not match password
elif request.form.get("password") != request.form.get("confirm-password"):
return apology("must match new password", 403)
# update database for username and hash new password
rows = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))
# Give user id number
session["user_id"] = rows[0]["id"]
# Redirect user to portfolio page
return redirect("/portfolio")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
for register.html
{% extends "layout.html" %}
{% block title %}
Register
{% endblock %}
{% block main %}
<form action="/register" method="post">
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="username"
placeholder="Username" type="text">
</div>
<div class="form-group">
<input class="form-control" name="password" placeholder="Password" type="password">
</div>
<div class="form-group">
<input class="form-control" name="confirm-password" placeholder="Confirm Password"
type="password">
</div>
<button class="btn btn-primary" type="submit">Register</button>
</form>
{% endblock %}
Nevermind, I figured it out for the register section.
I took out this line
# update database for username and hash new password
rows = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))
I decided to rename my 'confirm-password' to 'confirm_password' just to maintain some level of similarity in the code. But more importantly, it worked when I used these lines of code instead:
# if confirm password does not match password
elif request.form.get("password") != request.form.get("confirm_password"):
return apology("password does not match")
# Hash password / Store password hash_password =
hashed_password = generate_password_hash(request.form.get("password"))
# Add user to database
result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
username = request.form.get("username"),
hash = hashed_password)
if not result:
return apology("The username is already taken")
rows = db.execute("SELECT * FROM users WHERE username = :username",
username = request.form.get("username"))
I also recently found that if I did this cause I wanted to keep index(portfolio link) separated from /(homepage link):
@app.route("/")
def start():
return render_template("login.html")
before the @app.route(/index) section
In addition, I added onto line 29 in layout.html
<a class="navbar-brand" href="/"><span class="blue">C</span><span class="red">$</span><span class="yellow">5</span><span class="green">0</span> <span class="red">Finance</span></a>
these additional lines before and after it. So that it would not take me to the login page when I have already logged in after clicking on the home button:
{% if session.user_id %}
<a class="navbar-brand" href="/portfolio"><span class="blue">C</span><span class="red">$</span><span class="yellow">5</span><span class="green">0</span> <span class="red">Finance</span></a>
{% else %}
<a class="navbar-brand" href="/"><span class="blue">C</span><span class="red">$</span><span class="yellow">5</span><span class="green">0</span> <span class="red">Finance</span></a>
{% endif %}
I stopped having the 404 not found error