3

I', new to Flask and following the CS50 - Web Programming Course. My code is identical to the code ran by the lecturer, but it isn't working , leading me to believe I need some updates to my code since the video is 2 years old.

My problem is that the list in my code is not persisting between requests.

Here is my app.py

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"
#app.config['SECRET_KEY'] = 'super secret' Tried this didn't fix anything
Session(app)



@app.route("/", methods=["GET","POST"])
def index():
    print(session["notes"])
    if session.get("notes") is None:
        session["notes"] = []
    if request.method == "POST": # If the request method is POST, then I . . .
        note = request.form.get("note") # Get the note I am trying to add and...
        session["notes"].append(note) # Add it
        print(session["notes"])

    return render_template("index.html", notes=session["notes"]) # Return to the page the notes.

and my related index.html

{% extends 'layout.html' %}

{% block heading %}
    Notes
{% endblock heading %}

{% 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 body %}

Any one have any clues why it is not persisting?

This is also what my terminal outputs between Requests

127.0.0.1 - - [18/May/2020 20:30:12] "GET / HTTP/1.1" 200 -
[]        ---- Empty on first run, good so far
127.0.0.1 - - [18/May/2020 20:30:14] "POST / HTTP/1.1" 200 -
['Y']     ---- Y added, also good
127.0.0.1 - - [18/May/2020 20:30:16] "POST / HTTP/1.1" 200 -
[]        ---- Y gone! What happened?!?!

Thanks for any help you can give!

Caleb Renfroe
  • 183
  • 1
  • 13
  • I'm not able to reproduce your issue. I had to take out the initial `print(session["notes"])` under your route, since that throws an error if you try to print it before it's defined. Aside from that, though, I recreated your two files in the appropriate locations and replaced the "extends" block with regular HTML tags. It behaves as expected. Your log confuses me, though. How do you get 'Y' in the list without a preceding POST request? – Mike May 19 '20 at 03:54
  • You are correct, There is a POST request before it, I believe I somehow missed copying it in. I have updated the output to reflect it better. For me, I still am unable to get the program to execute correctly, and it is not building a list, but instead overwriting it with the new input. – Caleb Renfroe May 19 '20 at 17:37
  • @CalebRenfroe did you find a solution to it? even m stuck here – Monika Nishad Jun 07 '20 at 15:16
  • This is a duplicate you will find here [link](https://stackoverflow.com/questions/61972873/flask-session-lost-data/61977128#61977128) by author @ngShravil.py – Taymour Niazi Jun 12 '20 at 10:33

0 Answers0