0

after I run check50 I get following comments: enter image description here

Below are my code for register:

@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
session.clear()

if request.method == "POST":

    # Ensure username was submitted
    if not request.form.get("username"):
        return apology("must provide username")

    # Ensure password was submitted
    elif not request.form.get("password"):
        return apology("must provide password")

    elif not request.form.get("confirmation"):
        return apology("must confirm your password")

    elif request.form.get("passowrd") != request.form.get("confirmation"):
        return apology("password don't match")

    # Add username and password to database
    rows = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))

    rows = db.execute("SELECT * FROM users WHERE username = :username",
                      username=request.form.get("username"))
    if len(rows) != 1:
        return apology("username already in use")

    session["user_id"] = rows[0]["id"]

    return redirect("/")

else:
    return render_template("register.html")

I've completed all function but since the register function does not work, I could not check the correctness of my other functions. I could not figure out what's wrong with my code. Any suggestion would be much appreciated. Thank you!

Updated Code:

    result = db.execute("INSERT INTO users (username, hash) VALUES(:username, 
    :hash)", username=request.form.get("username"), 
    hash=generate_password_hash(request.form.get("password")))

    if not result:
        return apology("username already in use")

    rows = db.execute("SELECT * FROM users WHERE username = :username",
                      username=request.form.get("username"))

    session["user_id"] = rows[0]["id"]

    return redirect("/")

else:
    return render_template("register.html")

1 Answer 1

0

The first fail is probably because of the typo in this line elif request.form.get("passowrd") != request.form.get("confirmation"):. Check the spelling of your request.form variables carefully. As written, this will always evaluate to true.

The second fail is probably because the test for a duplicate is too late. If you successfully insert a user, and then retrieve the user, it will always be there.

6
  • Hi, I corrected the typo and updated my code. But check50 still fails at these two sections and says "sending POST request to /register exception raised in application: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. "
    – Issa
    Commented Sep 2, 2018 at 22:10
  • And you are not getting the same error when you try to register a new user? Assuming your app returns the error (which is what I would expect), you can review the trace that is dumped in the flask terminal. It should point you in the right direction of where to look for your error. Commented Sep 2, 2018 at 22:35
  • honestly I haven't tried flask run yet. I assumed the errors are due to my code in application.py, but I just could not figure out where
    – Issa
    Commented Sep 2, 2018 at 22:42
  • Okay so i tried flask run and tried to register a new user. It shows the error message "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" So I am assuming its my return redirect code not right. I tried return redirect("\index"), return redirect("\login") and return redirect(url_for("index")), they all get the same error messages. So I dont know what should i put there
    – Issa
    Commented Sep 2, 2018 at 22:46
  • return redirect("/") should "call" the index route. So the problem is probably in index function. review the flask terminal for errors. If you have problems with index, that should probably be logged in another post. Commented Sep 2, 2018 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .