我是Flask的新手,并遵循CS50-Web编程课程。我的代码与讲师运行的代码相同,但无法正常工作,这使我相信我需要对代码进行一些更新,因为该视频已有2年的历史。
我的问题是我的代码列表在请求之间不存在。
这是我的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.
和我相关的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 %}
有人知道为什么它不持久吗?
这也是我的终端在两次请求之间输出的信息
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?!?!
感谢您的帮助!