Python Flask+Bootstrap+Jinja2 构建轻量级企业内部系统平台框架

栏目: CSS · CSS3 · 发布时间: 6年前

内容简介:(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;

Python Flask+Bootstrap+Jinja2 构建轻量级企业内部系统平台框架

4.视图页面效果;

Python Flask+Bootstrap+Jinja2 构建轻量级企业内部系统平台框架


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

运营之光

运营之光

黄有璨 / 电子工业出版社 / 2016-9-1 / 59.00元

在互联网行业内,“运营”这个职能发展到一定阶段后,往往更需要有成熟的知识体系和工作方法来给予行业从业者们以指引。 《运营之光:我的互联网运营方法论与自白》尤其难得之处在于:它既对“什么是运营”这样的概念认知类问题进行了解读,又带有大量实际的工作技巧、工作思维和工作方法,还包含了很多对于运营的思考、宏观分析和建议,可谓内容完整而全面,同时书中加入了作者亲历的大量真实案例,让全书读起来深入浅出、......一起来看看 《运营之光》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具