0

As far as I can tell, I have the value set - username = username. Am I missing something stupid here?

Code:

 @app.route("/register", methods=["GET", "POST"])
    def register():
        """Register user"""
    
        if request.method == "GET":
            return render_template("register.html")
        else:
            # Take input for username
            username = request.form.get("username")
            #If username is blank return apology
            if not username:
                return apology("You must enter a username.")
    
            # Checks to see if username exists already, if so return apology
            usernamecheck = db.execute('SELECT EXISTS(SELECT * FROM users WHERE username = %s)', [username])
            if not usernamecheck:
                return apology("Username already exists.")
    
            # Take input for password
            password = request.form.get("password")
    
            # if password is blank, return apology
            if not password:
                return apology("You must enter a password.")
    
            # Take input for confirmation
            confirmation = request.form.get("confirmation")
    
            # if confirmation is blank, return apology
            if not confirmation:
                return apology("You must confirm your password.")
    
            # if password and confirmation don't match, return apology
            if password != confirmation:
                return apology("Your password confirmation does not match the password.")
    
    
            # hash the password with generate_password_hash
            pwhash = generate_password_hash(request.form.get("password"))
    
            # insert the username and hash into the users table
            db.execute("INSERT INTO users (username, pwhash VALUES (:username, :pwhash), username = username, pwhash = pwhash)")
            return redirect("/")

Error:

DEBUG:cs50:SELECT EXISTS(SELECT * FROM users WHERE username = 'test')
ERROR:application:Exception on /register [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/finance/application.py", line 162, in register
    db.execute("INSERT INTO users (username, pwhash VALUES (:username, :pwhash), username = username, pwhash = pwhash)")
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 219, in execute
    raise RuntimeError("missing value for placeholder (:{})".format(name))
RuntimeError: missing value for placeholder (:username)
0

Well I'm not very sure what %s do wrong, but I will use this instead:

usernamecheck = db.execute('SELECT EXISTS(SELECT * FROM users WHERE username=:uname)', uname=username)

where the :uname is the named placeholder, then you set uname to be the python variable whose value you want it to replace the placeholder (in this case, username).

Sometimes I use numbered placeholder, in case I just want one placeholder.

usernamecheck = db.execute('SELECT EXISTS(SELECT * FROM users WHERE username=?)', username)

Hope it helps!

1
  • Didn't work, returns a similar error. RuntimeError: missing value for placeholder (:username) Jun 26 '20 at 13:58

You must log in to answer this question.

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