I have implemented all functions. However when i run check50, it gets following errors
The errors might come from my index code. I am not sure. Below are my code for register and index. Any suggestion would be much appreciated.
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
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")
elif not request.form.get("confirmation"):
return apology("must confirm your password")
elif request.form.get("password") != request.form.get("confirmation"):
return apology("password don't match")
# Add username and password to database
result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))
if not result:
return apology("username already in use")
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
session["user_id"] = rows[0]["id"]
return redirect("/")
else:
return render_template("register.html")
Index:
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
rows_pt = db.execute("SELECT * FROM portfolio WHERE id = :id",id = session["user_id"])
if rows_pt == []:
cash_b = db.execute("SELECT cash FROM users WHERE id = :id",id = session["user_id"])
cash = cash_b[0]["cash"]
return render_template("index.html", current_balance=cash, total_balance = cash)
else:
new_rows_pt = []
for row in rows_pt:
new_rows_dict = {}
quote = lookup(row["symbol"])
new_rows_dict["symbol"] = quote["symbol"]
new_rows_dict["name"] = quote["name"]
new_rows_dict["shares"] = row["shares"]
new_rows_dict["price"] = quote["price"]
new_rows_dict["total"] = row["shares"] * quote["price"]
new_rows_pt.append(new_rows_dict)
cash_b = db.execute("SELECT cash FROM users WHERE id = :id",id = session["user_id"])
cash = cash_b[0]["cash"]
total_balance = cash
for row in new_rows_pt:
total_balance += row["total"]
return render_template("index.html", rows_pt=new_rows_pt, current_balance=cash, total_balance = cash)