'Key error when clicking on /viewcart in python flask

I am working with sessions. When the user clicks on /viewtask I want it to validate if there is an email in session. If there isnt my program should redirect the user to the login page. If there is then I want the user to view the cart. When I test this I get a 'Key error : Email'

I think its ignoring the if statement. I have no issue assigning a session something but I keep getting errors when checking the session.

App.py

@app.route("/viewcart")
def viewcart():
if session.get("Email") == None:
    return redirect(url_for(loginform))
else:
    conn = sqlite3.connect("data.db")
    c = conn.cursor()
    c.execute("SELECT ID FROM Users WHERE Email = '" + session['Email'] + "'")
    UserID = c.fetchone()[0]
    c.execute("SELECT Products.Name , Products.Price , Products.Image , Cart.Size FROM Products,Cart WHERE Products.ID == Cart.ProductID AND Cart.UserID == ?",(str(UserID)))
    products = c.fetchall()

    
    return render_template("cart.html",products = products)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source