dianzan.py
# 引入框架
from flask import Flask, render_template, request
app = Flask(__name__)
data = [
{"id": 0, "name": "中秋节:", "num": 0},
{"id": 1, "name": "国庆节", "num": 0},
{"id": 2, "name": "元旦节", "num": 0},
]
@app.route("/index")
def index():
return render_template("index.html", data=data)
@app.route("/dianzan")
def dianzan():
id = request.args.get("id")
# 字符串转换为整数 使用整数索引从data字典中获取键为2的值
data[int(id)]["num"] += 1
# print(f"想给{id}点赞")
return render_template("index.html", data=data)
app.run(debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>点赞系统</title>
</head>
<body>
<h1>点赞系统</h1>
<!-- {{data}} -->
<table border="1">
<tr>
<td>id</td>
<td>名字</td>
<td>点赞数</td>
<td>操作</td>
</tr>
{%for i in data%}
<tr>
<td>{{i.id}}</td>
<td>{{i.name}}</td>
<td>{{i.num}}</td>
<td><a href="/dianzan?id={{i.id}}">点赞</td>
{%endfor%}
</tr>
</table>
</body>
</html>