用flask html5帮我写个编辑画面
很高兴为您提供这个编辑画面的 Flask 和 HTML5 代码。
Flask 代码:
from flask import Flask, render_template, request, redirect, url_for
import os
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/edit", methods=["GET", "POST"])
def edit():
if request.method == "POST":
text = request.form["text"]
with open("text.txt", "w") as f:
f.write(text)
return redirect(url_for("index"))
else:
try:
with open("text.txt", "r") as f:
text = f.read()
except FileNotFoundError:
text = ""
return render_template("edit.html", text=text)
if __name__ == "__main__":
app.run(debug=True)
HTML5 代码:
<!DOCTYPE html>
<html>
<head>
<title>编辑画面</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f1f1f1;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
textarea {
width: 100%;
height: 400px;
border: none;
border-radius: 5px;
padding: 10px;
font-size: 16px;
font-family: Arial, sans-serif;
resize: none;
}
button {
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<div class="container">
<h1>编辑画面</h1>
<form method="POST" action="{{ url_for('edit') }}">
<textarea name="text">{{ text }}</textarea>
<button type="submit">保存</button>
</form>
</div>
</body>
</html>
这个编辑画面有一个文本框和一个保存按钮。当用户输入文本并点击保存按钮时,文本将被保存到 text.txt 文件中,然后重定向到首页。当用户访问编辑画面时,文本将从 text.txt 文件中读取并显示在文本框中。如果文件不存在,则文本框将保持空白。
原文地址: https://www.cveoy.top/t/topic/bpee 著作权归作者所有。请勿转载和采集!