Issue
Beginner-ish flask user here, and i can't find where i went wrong. i am routed to (/register.html) instead of (/register) so i know it's a blunder i made somewhere but everything looks alright to me, a pair of fresh eyes help me out.
Adding this because stackoverflow thinks my question is mostly code and wants me to add more details but i think my question is clear enough so please ignore this part.
@app.route("/register", methods=["POST", "GET"])
def register():
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Get username and password from form
username_submitted = request.form.get("username")
password_submitted = request.form.get("pasword")
# Query database for user
user = users.query.filter_by(username=username_submitted).first()
# Ensure a username was submitted
if not username_submitted or password_submitted:
flash("must provide username and password", category="error")
return redirect("/register")
# Ensure the username doesn't exist
elif len(user) != 0:
flash("username alredy exists", category="error")
return redirect("/register")
else:
# Hash password
hash = generate_password_hash(password_submitted)
# Insert new user into database
new_user = users(username=username_submitted, password=hash)
try:
db.session.add(new_user)
db.session.commit()
# Redirect user to homepage
return redirect("/")
except:
return "error signing you up"
# User reached route via GET (as by clicking a link or redirect)
else:
return render_template("register.html")
Here's my register form:
{% extends 'layout.html' %}
{% block title %}
Sign up
{% endblock %}
{% block main %}
<form action="/register" method="post">
<div class="form-container">
<div class="form">
<div class="username">
<input type="text" name="username" placeholder="Username" id="username" autofocus/>
</div>
<div class="password">
<input type="password" name="password" placeholder="Password" id="password"/>
</div>
</div>
<input type="submit" name="register" id="register" class="btn" value="Sign up"/>
</div>
</form>
{% endblock %}
Solution
I was able to solve this problem a few days ago the issue was that the href on my anchor tags that link to the login and signup page looked like this:
<a href='login.html'>Login</a>
instead of:
<a href='login'>Login</a>
Answered By - jeddah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.