python实战项目 点赞系统源码

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值