内容简介:(2).数据表结构; (和sql语句):4.视图页面效果;
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 = 'xxxx@163.com' 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'新增成功' + "\n" "日期" + "\n" + 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.视图页面效果;
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
JS 压缩/解压工具
在线压缩/解压 JS 代码
HEX HSV 转换工具
HEX HSV 互换工具