I get this error in register in pset7/finance when I run check50:

:) application.py exists
:) application starts up
:) register page has all required elements
:( registering user succeeds
   expected status code 200, but got 400
:) registration with an empty field fails
:) registration with password mismatch fails
:( registration rejects duplicate username
   expected status code 200, but got 400
:) login page has all required elements

In reality, the function works just fine. Here is my code:

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

  if request.method == "POST":

    # # ensure username was submitted
    # if not request.form.get("username"):
    #     return apology("Must provide username", 400)

    # # ensure password was submitted
    # if not request.form.get("password"):
    #     return apology("Must provide password", 400)

    # # ensure password and verified password is the same
    # if request.form.get("password") != request.form.get("passwordagain"):
    #     return apology("password doesn't match", 400)

    if request.form.get("username") != True or request.form.get("password") != True or request.form.get("password") != request.form.get("confirmation"):
        return apology("retry", 400)

        # insert the new user into users, storing the hash of the user's password
        result = db.execute("INSERT INTO users (username, hash) \
                             VALUES(:username, :hash)", \
                             username=request.form.get("username"), \

        # if not result:
        #     return apology("Username already exist")

        # remember which user has logged in
        session["user_id"] = result

        # redirect user to home page
        return redirect(url_for("index"), 200)

    return render_template("register.html")

I've wasted quite some time on this, and am really stuck. Thanks in advance!

1 Answer 1

    if request.form.get("username") != True or request.form.get("password") != True or request.form.get("password") != request.form.get("confirmation"):

request.form.get("username") and request.form.get("password") are strings. Instead of != True, use like if not request.form.get("username") or not request.form.get("password") or .... An empty string or even None in case your form did not pass a username at all, are both considered falsy in Python.

Also, no need for an else: in this case, as a return leaves the function. So you can happily do like

if this_mistake:
    return apology("baka")
if that_mistake:
    return apology("idiot")
if another_mistake:
    return apology("I think you must have messed it up")
# You won't reach this point if you made one of the three mistakes
  • I've done all of that. This still doesn't work. Jun 14, 2018 at 6:31
  • url_for seems to be another problem lately, probably due to how the cloud9 connection works. I had to replace (url_for("index") with "/"). Shouldn't be 400, though. And if you don't mind, please add the code where you"'ve done all of that" to your question.
    – Blauelf
    Jun 14, 2018 at 9:48
  • @Bauelf Which code? This is the whole of the register() function. Jun 14, 2018 at 19:44
  • I meant the part with the != True. What does it look like after fixing?
    – Blauelf
    Jun 15, 2018 at 5:55
  • I solved the problem by myself. Sorry for wasting your time. Jun 15, 2018 at 11:18

You must log in to answer this question.

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