Python Flask+Bootstrap+Jinja2 构建轻量级企业内部系统平台框架
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Flask+Bootstrap+Jinja2 构建轻量级企业内部系统平台框架相关的知识,希望对你有一定的参考价值。
1.最近公司运维需求,学习python flask 满足环境治理系统的开发;入门如下; 环境类型: Python 2.7.5 获取地址:https://www.python.org/downloads/ Flask 1.0.2 pip 安装使用模快如下; flask==1.0.2 request==1.0.2 Jinja2==2.10 Flask-Cors==3.0.6 MySQL-python==1.2.3 Bootstrap 4.0 获取地址:https://v4.bootcss.com/docs/4.0/getting-started/download/ 开发IDE 工具 pycharm Professional 获取地址:https://www.jetbrains.com/pycharm/ 2.启动flask 实例代码如下; 代码功能如下: (1).访问允许跨域 (2).静态资源目录定义 (3).模板渲染; (4).路由解析 (5).用户表单提交和校验; (6).数据层处理; # -*- coding: utf-8 -*- import sys import time from flask import Flask, request, render_template, Response from flask_cors import CORS from models import User ###需要安装 pip install -U flask-cors 模块# 解决跨域模块### reload(sys) sys.setdefaultencoding('utf-8') ##############定义静态文件存放路径######################## # app app = Flask(import_name=__name__, static_folder='./static', static_url_path='/static', template_folder='./templates') CORS(app, resources=r'/*') # r'/*' 是通配符,让本服务器所有的URL 都允许跨域请求 ######### @app.route('/', methods=['GET', 'POST']) ####默认首页########### def index(): class Person(object): Email = '[email protected]' gender = 'male' time = time.asctime(time.localtime(time.time())) dell = Person() context = { 'username': 'breaklinux', 'password': '123456', 'age': '25', 'hobby': 'python', 'person': dell, 'wwwurl': { 'baidu': 'www.baidu.com', 'google': 'www.google.com' } } return render_template('html/index.html', **context) # ##################命名规范flask 路由解析############## @app.route('/naming', methods=['GET', 'POST']) def naming(): Query_tow = User() Naming_List_name = Query_tow.Query_naming("环境治理类") Naming_List_name_nuber2 = Query_tow.Query_naming("发布规范类") return render_template('html/naming.html', Naming_List_name=Naming_List_name, Naming_List_name_nuber2=Naming_List_name_nuber2) @app.route('/create_naming', methods=['GET', 'POST']) def create_naming(): return render_template('html/create_naming.html') @app.route("/sigin_naming", methods=['GET', 'POST']) def sigin_naming(): ##########get 方法######request.args ##########post 方法#####rquest.from###### ################获取用户表单输入数据########### naming_name = request.args['naming_name'] naming_url = request.args['naming_url'] naming_type = request.args['naming_type'] create_founder = request.args['naming_create_founder'] ##################获取当前提交时间################### create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ####################判断表单用户输入是否为空,为空跳转当前页面############ if ("" == naming_name) or ("" == naming_url) or ("" == create_founder): return render_template('html/create_naming.html') else: #############实例化 models 文件 User 类和携带用户提交表单参数################ user = User() user.save(naming_type,naming_name, naming_url, create_time, create_founder) ##############调用class 类中save方法################ return Response(u'新增成功' + " " "日期" + " " + create_time) if __name__ == '__main__': app.run( host="0.0.0.0", port=80, debug=True ) 2.views 视图展示层; <html> <head> <meta charset="UTF-8"> <title>嫦娥发布规范文档</title> <link rel="stylesheet" href="static/css/bootstrap.min.css"> <script type="text/javascript" src="static/js/jquery.slim.min.js"></script> <script type="text/javascript" src="static/js/popper.min.js"></script> <script type=type="text/javascript" src="static/js/bootstrap.min.js"></script> </head> <style> body { background-color:#CCCCCC; margin:0px; padding:0px } </style> </head> <div> <div> <a href='/naming'> <br> <input type="button" class="btn btn-info" value="嫦娥命名规范"> </a> <hr > </div> <div> <br> <a href='/create_naming'> <input type="button" class="btn btn-primary" value="新建规范"> </a> </div> </div> <br> <br> <div> <div> <div> <a href='/deploy'> <input type="button" class="btn btn-success" value="环境部署"> </a> <hr> <a href='/component'> <input type="button" class="btn btn-warning" value="组件管理"> </a> <hr> <a href='/container'> <input type="button" class="btn btn-secondary" value="容器模板"> </a> <hr> <a href='/script'> <input type="button" class="btn btn-info" value="脚本管理 "> </a> <hr> <a href='/naming'> <input type="button" class="btn btn-danger" value="命名规范"> </a> </div> <div> </div> </div> <div> <div> <div> <div> <div style="height: 700px;width:auto"> <h2>环境治理类</h2> <br> {% for item in Naming_List_name %} <button onclick="window.location.href='{{item[1]}}'" type="button" class="btn btn-info">{{item[0] }}   <br> </button> {% endfor %} </div> </div> </div> <div> <h2> 集成发布类</h2> <br> {% for item in Naming_List_name_nuber2 %} <button onclick="window.location.href='{{item[1]}}'" type="button" class="btn btn-danger">{{ item[0]}} </button> {% endfor %} </div> </div> </div> </div> </body> 3.数据层; (1).数据层处理代码; # -*- coding: utf-8 -*- import MySQLdb def get_conn(): host = "192.168.xx.xx" port = 3306 db = "Scier" user = "root" password = "123456" conn = MySQLdb.connect(host, user, password, db, port, charset='utf8') return conn class User(object): def save(self,type, name, url, create_time, create_founder): self.type = type self.name = name self.url = url self.create_time = create_time self.create_founder = create_founder conn = get_conn() cursor = conn.cursor() sql = ("insert into naming (type,name,url,create_time,create_founder) VALUES (%s,%s,%s,%s,%s)") data = (self.type, self.name, self.url, self.create_time, self.create_founder) try: cursor.execute(sql, data) conn.commit() cursor.close() conn.close() except: cursor.close() conn.close() return "Error: unable to table namin" def Query_naming(self,type1): conn = get_conn() cursor = conn.cursor() self.type1 = type1 # SQL 查询语句命名规范名称和url地址字段####### sql = "SELECT NAME,url FROM naming WHERE TYPE='%s'" % self.type1 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() cursor.close() conn.close() return results except: cursor.close() conn.close() return "Error: unable to fecth data"
(2).数据表结构; (和sql语句):
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `naming` -- ---------------------------- DROP TABLE IF EXISTS `naming`; CREATE TABLE `naming` ( `create_time` datetime NOT NULL, `id` int(111) unsigned NOT NULL AUTO_INCREMENT, `type` varchar(20) COLLATE utf8mb4_bin NOT NULL, `name` varchar(20) COLLATE utf8mb4_bin NOT NULL, `url` varchar(100) COLLATE utf8mb4_bin NOT NULL, `create_founder` varchar(20) COLLATE utf8mb4_bin NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
4.视图页面效果;
以上是关于Python Flask+Bootstrap+Jinja2 构建轻量级企业内部系统平台框架的主要内容,如果未能解决你的问题,请参考以下文章
python flask 如何读取数据库数据并返回到html
Psutil + Flask + Pyecharts + Bootstrap 开发动态可视化系统监控
Flask实战-留言板-使用Bootstrap-Flask简化页面编写