0

I am just getting started on my register route in Pset7 and am getting a RuntimeError that I don't understand. Here is my register route code:

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

# forget any user_id
session.clear()

# if 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")

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

    # confirm password
    if request.form.get("password") is True:
        request.form.get("confirm_password")

    if request.form.get("password") != request.form.get("confirm_password"):
        return apology("passwords do not match")

    else:
        # insert user into database
        db.execute("INSERT INTO users (username, hash), VALUES(:username, :hash)",
            username=request.form["username"], hash=pwd_context.encrypt(request.form["password"]))

    # remember which user has logged in
    session["user_id"] = rows[0]["id"]

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

# else if user reached route via GET (as by clicking a link or via redirect)
else:
    return render_template("register.html")

And here is the RuntimeError I am getting:

File "/home/ubuntu/workspace/pset7/finance/application.py", line 129, in register
    username=request.form["username"], hash=pwd_context.encrypt(request.form["password"]))
File "/usr/lib/python3/dist-packages/cs50/sql.py", line 41, in execute
    raise RuntimeError(e)
RuntimeError: (sqlite3.OperationalError) near ",": syntax error [SQL: "INSERT INTO users (username, hash), VALUES('example', '$6$rounds=656000$kUKTtRa1TKWYdnxJ$N.q6IoRvBtMalH849KXGiO.OBiH5JUBWLn2rSChM.HrMqwVlfvup6spudC1ihlleQpCNeyQ1D7rgQI9G9Ffyj1')"]

It appears that it is unable to store the hashed password in the database, but I am unsure what changes to make to allow that to happen.

0

Look more closely, you switched from correct request.form.get("username") to wrong request.form["username"] at some point, same for password.

Both ways of accessing the form fields are valid. I just did not know what request.form is, it's a multidict, which has get method and bracket syntax like a regular dict.

Maybe the real issue: the comma in front of VALUES.

I used hash, not encrypt. Not sure about the implications.

You don't check for an existing username.

This code does not make any sense to me:

if request.form.get("password") is True:
    request.form.get("confirm_password")

but it won't hurt either, as request.form.get should not have side-effects.

4
  • Changing request.form["username"] to request.form.get("username") and the same for password does nothing and give the same RuntimeError. I know that I don't check for an existing username, I haven't that far in writing my code yet. Good catch, that code does not make any sense, I simply took it out. Yet, I am still unable to store the username and password in my database. Mar 17 '17 at 4:47
  • I don't seem to have read the error message. Have you tried without the comma before VALUES?
    – Blauelf
    Mar 17 '17 at 7:15
  • Yes! That comma was it! I noticed the comma in the error message, nut it said that the error was near ",". LOL, near means exactly! Thanks. Mar 17 '17 at 22:34
  • The parser wasn't exactly sure what you meant, and it's not meant to make "intelligent" guesses, those tend to be wrong sometimes. Thanks for giving me the opportunity to learn more about Python dicts, btw :)
    – Blauelf
    Mar 18 '17 at 9:26

You must log in to answer this question.

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