我正在尝试添加列表,但是它正在替换列表,您能看看我让我知道我做错了吗?
modules.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"
Session(app)
@app.route("/", methods=["GET","POST"])
def index():
if session.get("notes") is None:
session["notes"] = []
if request.method == "POST":
note = request.form.get("note")
session["notes"].append(note)
return render_template("index.html", notes=session["notes"])
以下为index.html
添加会话内容之前使用相同的代码
{% extends "layout.html"%}
{% block heading %}
Notes
{% endblock %}
{% 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 your notes" >
<button>Add Note</button>
</form>
{% endblock %}
似乎Flask仅支持字符串。因此,您需要将列表转换为字符串。您可以使用json或简单字符分隔列表(假设用户不输入该特定字符,例如逗号)。