0

I'm trying really hard to get register to work, it seems like when I try to use register, it doesn't do anything after I hit submit. I'm not sure why it does this or how to fix it but any help would be amazing as I'm really struggling with this. Here is my code:

def register():
"""Register user."""

session.clear()
if request.method == "POST": #POST CONDITIONAL

    #eturn render_template("register.html") # TRIED RETURNING TEMPLATE AT START

    if not request.form.get("username"):
        return apology("must provide username")

    elif not request.form.get("password"):
        return apology("must provide password")

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

    elif request.form.get("password") == request.form.get("password_confirm"):
        return apology("password must match password confirmation")

    hash = generate_password_hash(request.form.get("password"))

    result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash=hash)
    if not result:
        return apology("username is already taken")

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

    return render_template("index.html")


else:
    return render_template("register.html")


else:
    return render_template("register.html")

2 Answers 2

1

Assuming your register.html is correct, and you're actually submitting the form.

Executing return instantly leaves the function. Code below won't be executed.

What's the rationale behind the not in the first if? I think this makes everything wrong, executing the long part for GET and the short part at the end for POST.

The password confirmation field must have the name confirmation, not password_confirm. This is due to the fact that the tester will not use your form, but submit a prepared POST request. The correct field names are described in the problem statement.

INSERT returns an index, you'd probably need another SELECT.

10
  • how would I check to make sure I'm actually submitting the form?
    – Joel Banks
    Jun 11, 2018 at 1:01
  • Get into your browser's dev tools, usually F12, and watch the network tab while you submit. Depending on your settings, you should see either additional requests, or current content being replaced.
    – Blauelf
    Jun 11, 2018 at 5:58
  • I changed my code in the post to show what I've changed it to. It still isn't working though.I think its the SQL part. What can I do to change it?
    – Joel Banks
    Jun 12, 2018 at 14:54
  • You still have an instance of password_confirm. Also, you still need the SELECT to find the user_id.
    – Blauelf
    Jun 12, 2018 at 15:26
  • I struggle to understand a lot of the SQL work, mainly because it's new and I just need practice with it. Is it as easy as changing INSERT to SELECT or something of the like?
    – Joel Banks
    Jun 12, 2018 at 16:33
1

Check your code:

elif request.form.get("password") == request.form.get("password_confirm"):
    return apology("password must match password confirmation")

This is wrong: whenever the passwords match, it will return an apology. Change it with this:

elif request.form.get("password") !=request.form.get("password_confirm"):
    return apology("password must match password confirmation")

You must log in to answer this question.

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