Backend development with Flask: Retrieving Microblog entries from MongoDB (+4, -6)
app.py (+4, -6)
From:
curriculum/section07/lectures/15_retrieving_entries_mongodb/start/app.py
To:
curriculum/section07/lectures/15_retrieving_entries_mongodb/end/app.py
index c5ed6f6..ec0910f 100644
--- a/curriculum/section07/lectures/15_retrieving_entries_mongodb/start/app.py
+++ b/curriculum/section07/lectures/15_retrieving_entries_mongodb/end/app.py
@@ -10,7 +10,6 @@ load_dotenv()
app = Flask(__name__)
client = MongoClient(os.getenv("MONGODB_URI"))
app.db = client.microblog
-entries = []
@app.route("/", methods=["GET", "POST"])
@@ -18,15 +17,14 @@ def home():
if request.method == "POST":
entry_content = request.form.get("content")
formatted_date = datetime.datetime.today().strftime("%Y-%m-%d")
- entries.append((entry_content, formatted_date))
app.db.entries.insert_one({"content": entry_content, "date": formatted_date})
entries_with_date = [
(
- entry[0],
- entry[1],
- datetime.datetime.strptime(entry[1], "%Y-%m-%d").strftime("%b %d"),
+ entry["content"],
+ entry["date"],
+ datetime.datetime.strptime(entry["date"], "%Y-%m-%d").strftime("%b %d"),
)
- for entry in entries
+ for entry in app.db.entries.find({})
]
return render_template("home.html", entries=entries_with_date)