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"

notes = []

@app.route("/", methods=["GET", "POST"])
def index():
    if session.get("notes"):
        session["notes"] = []
    if request.method == "POST":
        note = request.form.get("note")
    return render_template("index.html", notes=session["notes"])

Here's the HTML:

{% block body %}
        {% for note in notes %}
            <li>{{ note }}</li>
        {% endfor %}

    <form action="{{ url_for('index') }}" method="POST">
        <input type="text" name="note" placeholder="Enter Note Here">
        <button>Add Note</button>
{% 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?

1 Answer 1


Try setting the first conditional in your index function to:

if session.get("notes") is None:

This way you will only reset the session's collection of notes when it actually did not yet exist.

At the moment you posted your code, you probably had set:

session["notes"] = "value"

earlier on, this resulted in the first conditional always evaluating to 'True', setting:

session["notes"] = []

Thus resetting your collection every time you posted a note. The posted note would then be appended and the only note in the collecion and thus also the only one displayed in your view (index.html).

  • Sadly I copypasted it wrong after a lot of debugging. My first conditional is actually "if session.get("notes") is None:" and it still doesn't work.
    – luk020
    May 25, 2020 at 0:33
  • did you come across an answer to this solution? i too am experiencing sessions['notes'] issues. i believe it has to do something with cookies/cache. i am using chrome.
    – Jabuc15
    Jul 1, 2020 at 6:52
  • it works for me Jul 8, 2020 at 5:44
  • I am wondering what is your python version, mine is 3.8.2 and it works. Jul 8, 2020 at 6:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .