定制软件基于Flask快速搭建一个管理系统

1. 定制软件用到的技术

1.1 导模块

from flask import Flask, render_template, jsonify, redirect, request, url_for, session

1.2 模块介绍

1.3 路由系统

1.3.1 普通使用

@app.route('/delete')

1.3.2 定制软件带有名分组

@app.route('/edit/<string:nid>', methods=['GET', 'POST'])  # 有名分组 默认string

1.3.3 定制软件带请求限制

@app.route('/login', methods=['GET', 'POST'])  # 默认支持get,定制软件需要手动添加post

1.3.4 带别名,定制软件默认别名是函数名

@app.route('/index', endpoint='index')  # 起别名,定制软件不写默认函数名称

1.3.5 定制软件路由其他参数(默认string)

  1. DEFAULT_CONVERTERS = {
  2. 'default': UnicodeConverter,
  3. 'string': UnicodeConverter,
  4. 'any': AnyConverter,
  5. 'path': PathConverter,
  6. 'int': IntegerConverter,
  7. 'float': FloatConverter,
  8. 'uuid': UUIDConverter,
  9. }

1.3.6 定制软件路由的本质

  1. 本质就是:app.add_url_rule()
  2. 定制软件路由加载的源码流程
  3. -将ur1和函数打包成为rule对象
  4. -将ru1e对象添加到map对象中。
  5. - app.url_map = map对象
  6. endpoint:如果不写默认是函数名,endpoint不能重名
  7. # 一般使用第一种
  8. @app.route('/index')
  9. def index():
  10. return render_template(' index.html ')
  11. # 第二种
  12. def home():
  13. return render_template(' index.html ')
  14. app.add_url_rule('/home', 'home', home)
  15. |

1.3.7 app.add_url_rule参数

  1. @app.route和app.add_url_rule参数:
  2. rule:URL规则
  3. view_func:视图函数名称
  4. defaults = None, 默认值, 当URL中无参数,函数需要参数时,使用defaults = {'k': 'v'}
  5. 为函数提供参数
  6. endpoint = None, 名称,用于反向生成URL,即: url_for('名称')
  7. methods = None, 允许的请求方式,如:["GET", "POST"]
  8. #对URL最后的 / 符号是否严格要求,默认严格,False,就是不严格
  9. strict_slashes = None
  10. '''
  11. @app.route('/index', strict_slashes=False)
  12. #访问http://www.xx.com/index/ 或http://www.xx.com/index均可
  13. @app.route('/index', strict_slashes=True)
  14. #仅访问http://www.xx.com/index
  15. '''
  16. #重定向到指定地址
  17. redirect_to = None,
  18. '''
  19. @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
  20. '''

1.4 获取提交的数据

  1. # 1. get请求
  2. nid = request.args.get('nid')
  3. # 2. post请求
  4. username = request.form.get('username')

1.5 返回数据

1.5.1 返回时的格式

  1. return render_template('login.html', error='用户名错误')
  2. return render_template('login.html', **{'error':'xxx','age':'xxx'})

1.5.2 坑1,出现找不到模板,解决办法

  • 项目下面是否有templates文件夹,你的html文件是否放进了里面;
  • templates文件夹是否和你运行的py文件在同一级目录;
  • render_template('***.html')这里面的名字是否正确,别打错了;
  • app = Flask(__name__, template_folder='templates') 在最开始的这句话中,template_folder后面一定要跟上templates;

1.6 &装饰器

1.6.1 导入&使用

  1. import functools
  2. from flask import session
  3. # flask使用session需要,传入一个secret_key,flask会生成一个key返回给前端,类似于cookie存储
  4. app.secret_key = 'lfsakhfnednlasdjmcls'
  5. def auth(func):
  6. @functools.wraps(func)
  7. def inner(*args, **kwargs):
  8. if session.get('username'):
  9. # print(session.get('username'))
  10. ret = func(*args, **kwargs)
  11. return ret
  12. return redirect(url_for('login'))
  13. return inner
  14. @app.route('/index', endpoint='index') # 起别名,不写默认函数名称
  15. @auth
  16. def index():
  17. """首页"""
  18. return render_template('index.html', data_dict=DATA_LIST)

1.6.2 装饰器的使用方法

  1. @app.before_requestdef
  2. def func():
  3. print('xxx')
  4. def x1():
  5. print('xxx')
  6. x1.app.before_request(x1)

1.7 request介绍

  • django的请求处理是逐一封装和传递;
  • flask的请求是利用上下文管理来实现的。

1.8 django/flask介绍

  • django是个大而全的框架,flask是一个轻量级的框架。 django内部为我们提供了非常多的组件:
orm / session / cookie / admin / form / modelform/路由/视图/模板/中间件/分页/ auth / contenttype/缓存/信号/多数据库连接
  •  flask框架本身没有太多的功能:路由/视图/模板(jinja2)/session/中间件,第三方组件非常齐全。

2. 快速搭建管理系统

2.1 基本使用

  1. from flask import Flask
  2. # 起一个名字
  3. app = Flask(__name__)
  4. # 配置路由
  5. @app.route('/index')
  6. def index():
  7. return 'hello word'
  8. if __name__ == '__main__':
  9. # 启动
  10. app.run()

2.2 完整版

2.2.1 py

  1. import functools
  2. from flask import Flask, render_template, jsonify, redirect, request, url_for, session
  3. app = Flask(__name__) # 默认 template_folder='templates'
  4. # 模拟数据
  5. DATA_LIST = {
  6. '1': {'name': 'a', 'age': 32},
  7. '2': {'name': 'a', 'age': 64},
  8. }
  9. # flask使用session需要,传入一个secret_key,flask会生成一个key返回给前端,类似于cookie存储
  10. app.secret_key = 'lfsakhfnednlasdjmcls'
  11. def auth(func):
  12. @functools.wraps(func)
  13. def inner(*args, **kwargs):
  14. if session.get('username'):
  15. # print(session.get('username'))
  16. ret = func(*args, **kwargs)
  17. return ret
  18. return redirect(url_for('login'))
  19. return inner
  20. @app.route('/login', methods=['GET', 'POST']) # 默认支持get,需要手动添加post
  21. def login():
  22. """登录"""
  23. # render_template, # 类似与django中国的render
  24. # jsonify, # 类似与django中国的JsonResponse
  25. # url_for 别名
  26. if request.method == 'GET':
  27. return render_template('login.html')
  28. username = request.form.get('username')
  29. password = request.form.get('password')
  30. if username == '1' and password == '1':
  31. session['username'] = username
  32. return redirect('/index')
  33. return render_template('login.html', error='用户名错误')
  34. @app.route('/index', endpoint='index') # 起别名,不写默认函数名称
  35. @auth
  36. def index():
  37. """首页"""
  38. return render_template('index.html', data_dict=DATA_LIST)
  39. @app.route('/edit/<string:nid>', methods=['GET', 'POST']) # 有名分组 默认string
  40. @auth
  41. def edit(nid):
  42. """编辑"""
  43. if request.method == 'GET':
  44. user_dic = DATA_LIST.get(nid)
  45. # print(user_dic) # {'name': 'a', 'age': 32}
  46. return render_template('edit.html', user_dic=user_dic)
  47. name = request.form.get('username')
  48. age = request.form.get('age')
  49. DATA_LIST[nid]['name'] = name
  50. DATA_LIST[nid]['age'] = age
  51. return redirect('/index')
  52. @app.route('/delete')
  53. @auth
  54. def delete():
  55. """删除"""
  56. nid = request.args.get('nid')
  57. print(nid) # 2
  58. del DATA_LIST[nid]
  59. return redirect(url_for('index'))
  60. if __name__ == '__main__':
  61. app.run()

2.2.2 login.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登录</title>
  6. </head>
  7. <body>
  8. <form method="post">
  9. <p>用户名:<input type="text" name="username"></p>
  10. <p>密码:<input type="password" name="password"></p>
  11. {{ error }}
  12. <input type="submit" value="提交">
  13. </form>
  14. </body>
  15. </html>

2.2.3 index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
  7. </head>
  8. <body>
  9. <div>
  10. <div class="bs-example" data-example-id="hoverable-table">
  11. <div class="panel panel-info">
  12. <div class="panel-heading">
  13. <h3 class="panel-title">xx管理系统</h3>
  14. </div>
  15. <div class="panel-body">
  16. <table class="table table-hover">
  17. <thead>
  18. <tr>
  19. <th>序号</th>
  20. <th>名称</th>
  21. <th>年龄</th>
  22. <th>操作</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. {% for key,item in data_dict.items() %}
  27. <tr>
  28. <th scope="row">{{ key }}</th>
  29. <td>{{ item.name }}</td>
  30. <td>{{ item["age"] }}</td>
  31. <td><a href="/edit/{{ key }}">编辑</a>|
  32. <a href="/delete?nid={{ key }}">
  33. 删除
  34. </a>
  35. </td>
  36. </tr>
  37. {% endfor %}
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  45. <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
  46. </body>
  47. </html>

2.2.4 edit.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>编辑</title>
  6. </head>
  7. <body>
  8. <form method="post">
  9. <p>用户名:<input type="text" name="username" value="{{ user_dic.name }}"></p>
  10. <p>密码:<input type="text" name="age" value="{{ user_dic.age }}"></p>
  11. <input type="submit" value="提交">
  12. </form>
  13. </body>
  14. </html>

3. 截图

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发