I'm using VS Code and following along Brian's Flask class in Web50.
I have copied his notes program word for word, and somehow I get different results. I've downloaded Flask and Sessions, so that's not the issue.
Here's the code:
from flask import Flask, render_template, request, session
from flask_session import Session
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
notes = []
@app.route("/", methods=["GET", "POST"])
def index():
if session.get("notes"):
session["notes"] = []
if request.method == "POST":
note = request.form.get("note")
session["notes"].append(note)
return render_template("index.html", notes=session["notes"])
Here's the HTML:
{% block body %}
<ul>
{% for note in notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
<form action="{{ url_for('index') }}" method="POST">
<input type="text" name="note" placeholder="Enter Note Here">
<button>Add Note</button>
</form>
{% endblock %}
The problem is that when I POST a new note, the previous one always disappear. If I print(session['notes']) at the start of index(), it always prints an empty list, even when I write a new note and the new note shows up on the page.
What am I doing wrong?