-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
43 lines (30 loc) · 1.22 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import Flask, render_template, request, redirect
from cs50 import SQL
app = Flask(__name__)
db = SQL("sqlite:///recommendations.db") # setting db
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST": # testing inputs
title = request.form.get("title")
author = request.form.get("author") or "unknown" # support for "unknown" authors
reason = request.form.get("reason")
# dealing with input errors:
if not title:
return "Please enter the book's name!"
elif not reason:
return "Please enter a reason. Any reason!"
# all good? save the data!
else:
db.execute("INSERT INTO recommendations (title, author, reason) VALUES(?, ?, ?)", title, author, reason)
return redirect("/recommendations")
else:
return render_template("index.html")
@app.route("/recommendations")
def recommendations():
# getting data from database
recs = db.execute("SELECT title, author, reason FROM recommendations ORDER BY title")
# sending recs data to template
return render_template("recommendations.html", recs=recs)
# starting the app
if __name__ == "__main__":
app.run()