Project 2: Habit Tracker: Displaying and saving habits (+20, -3)

app.py (+6, -2)

From: curriculum/section10/lectures/02_displaying_and_saving_habits/start/app.py

To: curriculum/section10/lectures/02_displaying_and_saving_habits/end/app.py

            
            index e72936c..ae48b8f 100644
--- a/curriculum/section10/lectures/02_displaying_and_saving_habits/start/app.py
+++ b/curriculum/section10/lectures/02_displaying_and_saving_habits/end/app.py
@@ -1,13 +1,17 @@
-from flask import Flask, render_template
+from flask import Flask, render_template, request
 
 app = Flask(__name__)
+habits = ["Test habit"]
 
 
 @app.route("/")
 def index():
-    return render_template("index.html", title="Habit Tracker - Home")
+    return render_template("index.html", habits=habits, title="Habit Tracker - Home")
 
 
 @app.route("/add", methods=["GET", "POST"])
 def add_habit():
+    if request.form:
+        habits.append(request.form.get("habit"))
+
     return render_template("add_habit.html", title="Habit Tracker - Add Habit")
        

add_habit.html (+2, -0)

From: curriculum/section10/lectures/02_displaying_and_saving_habits/start/templates/add_habit.html

To: curriculum/section10/lectures/02_displaying_and_saving_habits/end/templates/add_habit.html

            
            index 16b6a06..9d5807e 100644
--- a/curriculum/section10/lectures/02_displaying_and_saving_habits/start/templates/add_habit.html
+++ b/curriculum/section10/lectures/02_displaying_and_saving_habits/end/templates/add_habit.html
@@ -3,6 +3,8 @@
 {% block main_content %}
 
     <form class="form" method="POST">
+        <textarea class="form__input" id="habit" name="habit" rows="3" placeholder="Add a new daily habit"></textarea>
+        <button class="form__button" type="submit">Add</button>
     </form>
 
 {% endblock %}
        

index.html (+8, -1)

From: curriculum/section10/lectures/02_displaying_and_saving_habits/start/templates/index.html

To: curriculum/section10/lectures/02_displaying_and_saving_habits/end/templates/index.html

            
            index 72b96f7..5b835af 100644
--- a/curriculum/section10/lectures/02_displaying_and_saving_habits/start/templates/index.html
+++ b/curriculum/section10/lectures/02_displaying_and_saving_habits/end/templates/index.html
@@ -1,6 +1,13 @@
 {% extends "layout.html" %}
 
 {% block main_content %}
-    <section>
+    <section class="habit-list">
+    {% for habit in habits %}
+        <div class="habit">
+            <p class="habit__name">
+                {{ habit }}
+            </p>
+        </div>
+    {% endfor %}
     </section>
 {% endblock %}
        

layout.html (+4, -0)

From: curriculum/section10/lectures/02_displaying_and_saving_habits/start/templates/layout.html

To: curriculum/section10/lectures/02_displaying_and_saving_habits/end/templates/layout.html

            
            index b4215ed..08b597a 100644
--- a/curriculum/section10/lectures/02_displaying_and_saving_habits/start/templates/layout.html
+++ b/curriculum/section10/lectures/02_displaying_and_saving_habits/end/templates/layout.html
@@ -13,6 +13,10 @@
     
     <body>
         <header class="header">
+            <h1 class="header__logo"><a href="{{ url_for('index') }}" class="header__link">Habits</a></h1>
+            {% if "add" not in request.endpoint %}
+                <a href="{{ url_for('add_habit') }}" class="header__link">+ Add new</a>
+            {% endif %}
         </header>
 
         <main class="main">