我是 Flask 的新手,正在学习 CS50 - Web Programming Course。我的代码与讲师运行的代码相同,但它不起作用,这让我相信我需要对我的代码进行一些更新,因为该视频已有 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?!?!
谢谢你提供的所有帮助!