Backend development with Flask: Storing Microblog entries in MongoDB (+9, -1)
app.py (+9, -1)
From:
curriculum/section07/lectures/14_storing_entries_mongodb/start/app.py
To:
curriculum/section07/lectures/14_storing_entries_mongodb/end/app.py
index 36e391d..c5ed6f6 100644
--- a/curriculum/section07/lectures/14_storing_entries_mongodb/start/app.py
+++ b/curriculum/section07/lectures/14_storing_entries_mongodb/end/app.py
@@ -1,8 +1,15 @@
+import os
import datetime
from flask import Flask, render_template, request
+from pymongo import MongoClient
+from dotenv import load_dotenv
+
+load_dotenv()
-app = Flask(__name__)
+app = Flask(__name__)
+client = MongoClient(os.getenv("MONGODB_URI"))
+app.db = client.microblog
entries = []
@@ -12,6 +19,7 @@ def home():
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 = [
(