Python Flask+Bootstrap+Jinja2 構建輕量級企業內部系統平臺框架
阿新 • • 發佈:2018-07-03
集成 static lec text creat 文檔 button auto tex 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'新增成功' + "\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.視圖頁面效果;
Python Flask+Bootstrap+Jinja2 構建輕量級企業內部系統平臺框架