Backend development with Flask: Displaying past entries in the Microblog project (+15, -11)
app.py (+4, -2)
From:
curriculum/section07/lectures/10_display_past_entries_microblog/start/app.py
To:
curriculum/section07/lectures/10_display_past_entries_microblog/end/app.py
index 0374211..9dbdcc6 100644
--- a/curriculum/section07/lectures/10_display_past_entries_microblog/start/app.py
+++ b/curriculum/section07/lectures/10_display_past_entries_microblog/end/app.py
@@ -4,12 +4,14 @@ from flask import Flask, render_template, request
app = Flask(__name__)
+entries = []
+
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
entry_content = request.form.get("content")
formatted_date = datetime.datetime.today().strftime("%Y-%m-%d")
- print(entry_content, formatted_date)
+ entries.append((entry_content, formatted_date))
- return render_template("home.html")
+ return render_template("home.html", entries=entries)
home.html (+11, -9)
From:
curriculum/section07/lectures/10_display_past_entries_microblog/start/templates/home.html
To:
curriculum/section07/lectures/10_display_past_entries_microblog/end/templates/home.html
index 8511364..73ae245 100644
--- a/curriculum/section07/lectures/10_display_past_entries_microblog/start/templates/home.html
+++ b/curriculum/section07/lectures/10_display_past_entries_microblog/end/templates/home.html
@@ -33,15 +33,17 @@
</section>
<section>
<h1>Recent posts</h1>
- <article class="entry">
- <div>
- <h2 class="entry__title">A bit of a chill day today</h2>
- <time class="entry__date" datetime="24-10-2019">• Oct 24</time>
- </div>
- <p class="entry__content">
- Today I couldn't do much programming, but that's OK! Can't be too awesome every day now!
- </p>
- </article>
+ {% for entry in entries %}
+ <article class="entry">
+ <div>
+ <h2 class="entry__title">{{ entry[0] | truncate(30, true) }}</h2>
+ <time class="entry__date" datetime="{{ entry[1] }}">• {{ entry[1] }}</time>
+ </div>
+ <p class="entry__content">
+ {{ entry[0] }}
+ </p>
+ </article>
+ {% endfor %}
</section>
</main>
<footer class="footer">