I need to learn more about session. I have book route which goes to a book page stating the description of the book, reviews from other users and write a review. I create a form in html and submit my review to review route which will upload to my sql. The issue I am have is KEYERROR: 'isbn for my session['isbn'] is not recognized in my review route which i'm confused. How can i make session['isbn'] available throughout my code? is there a better way?. Thank you for your response. please let me know if you need more information. The code is below:

def book(isbn):
session["isbn"] = isbn
book = db.execute("SELECT * FROM books WHERE isbn = :isbn", {"isbn": session["isbn"]}).fetchone()
reviews = db.execute("SELECT * FROM reviews WHERE isbn = :isbn", {"isbn": isbn}).fetchall()
return render_template("book.html", book=book, reviews=reviews)

@app.route("/review", methods=["POST"])
def review():
review = request.form.get("review")
rate = request.form.get("rate")

row = db.execute("SELECT * FROM reviews WHERE user_id = :user_id AND isbn = :isbn",
                    {"user_id":session["user_id"], "isbn":session["isbn"]}).fetchone()

if row is not None:
    return apology("Review has been entered")

db.execute("INSERT INTO reviews(user_id, review, rate, isbn) VALUES (:user_id, :review, :rate, :isbn)",
            {"user_id":session["user_id"], "review":review, "rate":rate, "isbn":session["isbn"]})

return redirect("/book",isbn=session["isbn"])

You must log in to answer this question.

Browse other questions tagged .