一、flask服务器简单搭建

1.flask 主代码

import templates as templates
from flask import Flask, render_template, jsonify, request, redirect, url_for
import config

# 创建对象
app = Flask(__name__)
app.config.from_object(config)  # 使用配置文件

DATA_DICT = {
    1: {'name': '西奥', 'age': 73},
    2: {'name': '旺财', 'age': 84}
}

# 1.创建flask路由
# 2.('/', methods={'GET', 'POST'})括号里边儿带着的是flask的路由参数
# 3. 注意 endpoint 不能重名
@app.route('/', methods={'GET', 'POST'})
def login():
    if request.method == 'GET':
        return render_template('about.html')  # HttpResponse
    print(request.form)
    user = request.form.get('user')
    pwd = request.form.get('pwd')
    if user == '123' and pwd == '456':
        return redirect('/index')
    else:
        error = '用户名或密码错误'
        return render_template('about.html', error=error)


@app.route('/index', endpoint='idx')
def index():
    data_dict = DATA_DICT
    return render_template('index.html', data_dict=data_dict)

# 4.falsk使用动态路由
# 5. request.args 为GET形式传递的参数
# 6. request.form 为POST形式提交的参数
# @app.route('/edit/<int:nid>')
@app.route('/edit', methods={'GET', 'POST'})
def edit():
    nid = request.args.get('nid')
    nid = int(nid)
    if request.method == 'GET':
        info = DATA_DICT[nid]
        return render_template('edit.html', info=info)

    user = request.form.get('user')
    age = request.form.get('age')
    DATA_DICT[nid]['name'] = user
    DATA_DICT[nid]['age'] = age

    return redirect(url_for('idx'))
    
# 返回数据形式
# 1. return render_template('模板文件')
# 2. return jasonify
# 3. return redirect('/index')(传url) 
#或者 redirect(url_for('别名'))
# 4. return "字符串"

# 模板处理 for 循环
# 1. {% for key, value in data_dict.items() %}
#		{{iterm}}
#	 {% endfor %}
@app.route('/del/<int:nid>')
def delete(nid):
    del DATA_DICT[nid]
    # return redirect('/index')
    return redirect(url_for("idx"))


if __name__ == '__main__':
    app.run()

2.about.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>关于我们</title>
</head>
<body>
    <h1>用户登录</h1>
        <form method="post">
            <input type="text" name="user">
            <input type="text" name="pwd">
            <input type="submit" name="提交"><span style="color:red">
            {{error}}
        </span>
        </form>>
</body>
</html>

3.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>用户列表</h1>
    <table border="1">
        <thead>
        <tr>
            <th>ID</th>
            <th>用户名</th>
            <th>年龄</th>
            <th>操作</th>

        </tr>
        </thead>
        <tbody>
            {% for key, value in data_dict.items() %}
                <tr>
                    <td>{{key}}</td>
                    <td>{{value.name}}</td>
                    <td>{{value.age}}</td>
                    <td>
                        <a href = "/edit?nid={{key}}">编辑</a>
                        <a href = "/del/{{key}}">删除</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

4.edit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>修改</h1>
    <form method="post">
        <input type="text" name="user" value="{{info.name}}">
        <input type="text" name="age" value="{{info.age}}">
        <input type="submit"  value="提交">
    </form>

</body>
</html>

本章学习主要参考B站银角大王的学习视频,大多数也仅供本人学习参考,因为是初学,文章暂时不做大多阐述,想学习flask搭建的可以到B站学习,这是前6集的代码,有需要的可以直接复制。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

The endeavor

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值