模型部署flask学习篇(二)---- flask用户登录&用户管理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

在学习机器学习或者深度学习后,我们常常会将自己训练好的模型来进行部署或者可视化出来,供自己或者别人来使用,那么python的flask框架就可以作为选择之一。


一、用户登录&用户管理

1、 flask路由:可以接收get请求和post请求

python 复制代码
@app.route('/index',methods= ['GET','POST'])
def index():
    pass

2、动态路由

python 复制代码
@app.route('/index',methods= ['GET','POST'])
def index():
    pass
#可以在index后面传参数默认是str
@app.route('/index/<nid>',methods= ['GET','POST'])
def index():
    pass
# 转成int类型
@app.route('/index/<int:nid>',methods= ['GET','POST'])
def index():
    pass

3、获取提交的参数

python 复制代码
from flask import Flask,render_template,jsonify,request,redirect

@app.route('/index',methods= ['GET','POST'])
def index():
    request.args.get() # GET形式传递的参数
    request.form.get() # POST形式传递的参数

4、返回数据(四种形式)

python 复制代码
@app.route('/index',methods= ['GET','POST'])
def index():
    return render_template('index.html')
	return jsonify()
	return redirect('/index')
	return '字符串'

5、模板处理(也就是在html中)

python 复制代码
{{ x }} #插值语法
# 在html中写for循环
{% for item in list %}
	{{item}}
{% endfor %}

二、完整代码

python 复制代码
from flask import Flask,render_template,jsonify,request,redirect

app = Flask(__name__)

DATA_DICT = {
    '1':{'name':'张三','age':12},
    '2':{'name':'李四','age':13},
}
# 登陆页面
@app.route('/login',methods= ['GET','POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html') #render
    user = request.form.get('user')
    pwd = request.form.get('pwd')
    # 密码正确则进入index网页
    if user == 'zhaowentao' and pwd =='zwt':
        return redirect('/index')
    error = '用户名或密码错误'
    return render_template('login.html',error=error)
# 主页面
@app.route('/index')
def index():
    data_dict = DATA_DICT
    return render_template('index.html',data_dict=data_dict)
# 用户编辑页面
@app.route('/edit',methods=['GET','POST'])
def edit():
    # 获取nid
    nid = request.args.get('nid')
    info = DATA_DICT[nid]
    # 打开edit网页
    if request.method == 'GET':
        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
    # 跳转index网页
    return redirect('/index')
# 用户删除
@app.route('/del/<nid>')
def delete(nid):
    # 删除要删除的信息
    del DATA_DICT[nid]
    return redirect('/index')
# 运行
if __name__ == '__main__':
    app.run()

三、完整html代码

1、login.html

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">
        <input type="text" name="pwd">
        <input type="submit" name="提交"><span style="color:red;">{{error}}</span>
    </form>
</body>
</html>

2、index.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>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>

3、edit.html

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">
    <input type="text" name="age">
    <input type="submit" name="提交">
  </form>
</body>
</html>
相关推荐
花酒锄作田5 小时前
[python]argparse 包在聊天机器人中的应用
python
一尘之中7 小时前
从C语言底层设计到系统架构评估:软件架构知识体系全景
学习·系统架构·ai写作
NiceCloud喜云7 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
为思念酝酿的痛7 小时前
POSIX信号量
linux·运维·服务器·后端
小羊在睡觉7 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
AI玫瑰助手8 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
weixin_468466858 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
小糖学代码8 小时前
LLM系列:环境搭建:5.Python-dotenv 环境变量管理
人工智能·python·深度学习·神经网络
swipe8 小时前
Neo4j + Graph RAG 医疗知识图谱工程实践:患者教育问答真正需要的是“关系可追溯”
后端·langchain·llm
星夜夏空998 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb