So far I've implemented 'Register','Quote' and 'Buy' in applications.py. The Problem is, every time I log in and then query a quote or attempt to buy, I get the error message "Must provide username"(which is the error message from the'Log in' function) because apparently my session is not 'remembered'. I've also tried

session["user_id"] = result[0]["id"]

but this doesn't work either.

My Register Code:

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

    # if user reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # ensure username and password were submitted
        if request.form["username"] == "" or request.form["password"] == "" or request.form["confirm password"] == "":
            return apology("must fill out all fields")
        # ensure passwords match    
        if request.form["password"] != request.form["confirm password"]:
            return apology("your passwords don't match")

        # hash password

        # insert user into database and ensure username does not already exist
        result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash=hash)
        if not result:
            return apology("username already taken")

        # automatically log user in   
        # remember which user has logged in

        session["user_id"] = result

        # 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)
        return render_template("register.html")  
  • Are you logging in every time or registering every time? If you are logging in, then register is not called so it is not causing the problem. If you are registering every time, then you need to correct the posted code to match the IDE. This code is not indented properly, and will cause errors, the app will not run. To properly format the code: after it is pasted, select it and click the {} icon in the format bar. May 6 '17 at 17:33
  • The problem occurs both when I log in and when I register and then try to proceed to buy etc.
    – Bella
    May 6 '17 at 18:37

The problem is (probably) in quote.html and buy.html. Likely they have action attribute pointing to login like this <form action="{{ url_for('login') }}" method="post">

  • You are right – thank you so much!!!
    – Bella
    May 7 '17 at 8:09

You must log in to answer this question.

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