0

Whenever I try to log in with a registered user, I get a ValueError: View function did not return a response error. It doesn't make sense because I can see that the users are registered in my database and yet, there seems to be something wrong with the login function. Any help would be appreciated, thank you.

Here is my code so far

@app.route("/login", methods=["GET", "POST"]) def login(): """Log user in."""

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

    # query database for username
    rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))

    # ensure username exists and password is correct
    if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]):
        return apology("invalid username and/or 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("login.html")

@app.route("/logout") def logout(): """Log user out."""

# forget any user_id
session.clear()

# redirect user to login form
return redirect(url_for("login"))

@app.route("/quote", methods=["GET", "POST"]) @login_required def quote(): """Get stock quote."""

# Call on lookup to look up the price of stock for the company's symbol.
if request.method == "POST":
    rows = lookup(request.form.get("symbol"))

    # Ensures the symbol input by user exists or it's not blank.
    if not rows:
        return apology("Invalid Symbol")

    # return quoted.html, which displays the price of the share the user inquired about.
    return render_template("quoted.html", stock = rows)
else:
    return render_template("quote.html")

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

# clear previous session.
session.clear()

if request.method == "POST":

    # Ensure the user submitted a username.
    if not request.form.get("username"):
        return apology("must submit a username")

    # Ensure the user submitted a password.
    elif not request.form.get("password"):
        return apology("must submit a password")

    # Ensure the user retyped their password.
    elif not request.form.get("password-confirm"):
        return apology("must retype password")

    # Ensure that the passwords are the same.
    if request.form.get("password") != request.form.get("password-confirm"):
        return apology("passwords must be the same")

    # Store hash of the password.
    password = request.form.get("password")
    hashed = pwd_context.hash(password)

    # Ensure username is unique and doesn't already exist.
    result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username = request.form.get("username"), hash = hashed)
    if not result:
        return apology("Please choose another username")

    # Store the user id in the database.
    rows = db.execute("SELECT * FROM users WHERE username = :username", username = request.form.get("username"))
    session["user_id"]= rows[0]["id"]
    return redirect(url_for("index"))

else:
    return render_template("register.html")

return redirect(url_for("index"))
0

Sounds like this

return redirect(url_for("index"))

is not working.

Do you actually have an index route? How about you say

return redirect("/")

instead.

You must log in to answer this question.

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