0

When I don't include last two lines in code about"remembering user id" and redirect user to login the code is registering the user successfully into the table but when I enter these two lines in the code web server gives me this error "werkzeug.routing.BuildError: Could not build url for endpoint 'sell'. Did you mean 'login' instead?" While I try to login not to go to sell page, Kindly check what am I doing wrong. Rest of the code is default I didn't change

@app.route("/register", methods=["GET","POST"])
def register():
# 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
    if not request.form.get("password"):
        return apology("must provide password")
    #ensure again password submitted
    if not request.form.get("again_password"):
        return apology("must provide password again")
    #ensure both password match    
    if request.form.get("password") == request.form.get("again_password"):
        #query database for username
        rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
       #Ensure usename don't exist already
        if  len(rows) == 0:    
            #Encrypt password
            h = pwd_context.encrypt(request.form.get("password"))
            #insert username and password to SQL data base
            db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username=request.form.get("username"), hash=h)
            #Remember user id after successful register.
            session["user_id"] = rows[0]["id"]

            # redirect user to home page
            return redirect(url_for("index"))
        else:
            #return apology if username already exist
            return apology("Username already exist")
    else:
        #return apology if both password don't match with each other
        return apology("both password should match")
#Return to register.html and use post mathod        
else:
    return render_template("register.html")
1

How do you get here session["user_id"] = rows[0]["id"] ? When if len(rows) == 0: is true.
When is that true? When the SELECT * FROM users.... returns no rows.
If the select returns no rows, what is rows[0]["id']? An error waiting to happen.

This hint from the spec may be helpful:

If str is an INSERT, and the table into which data was inserted contains an autoincrementing PRIMARY KEY, then execute returns the value of the newly inserted row’s primary key.

12
  • key = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username=request.form.get("username"), hash=h) #Remember user id after successful register. session["user_id"] = key [0] ["id"]
    – Ahmed Raza
    Feb 18 '17 at 14:48
  • This is the change in code as you said @dinoCoderSaurus error is still same when it calls index page.
    – Ahmed Raza
    Feb 18 '17 at 14:49
  • the db.execute("INSERT..... does not return a list, it returns the value of the newly inserted row's primary key. Feb 18 '17 at 14:51
  • <pre>< key = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username=request.form.get("username"), hash=h) #Remember user id after successful register. session["user_id"] = key'>
    – Ahmed Raza
    Feb 18 '17 at 14:54
  • I also tried this but still not working @DinoCoderSaurus
    – Ahmed Raza
    Feb 18 '17 at 14:54

You must log in to answer this question.

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