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!