
作用:不用把代码都放到一个文件里,可以进行分类
baidu文件夹
|— baidu.py
|— app文件夹
—-|init.py(代表是python文件,这里算配置文件)
—-|abc.py
—-|static文件夹(静态文件)
—-|templates文件夹(html模版文件)
baidu.py
from app import create_appapp = create_app()if __name__=='__main__':#app.run(host='0.0.0.0',port=8000,debug=True)#app.run(host='0.0.0.0',port=8000)#app.run(host='127.0.0.1',port=8000,debug=True)app.run(host='127.0.0.1',port=8000)
自己修改端口号了
baidu/app/__init__.py
#flaskfrom flask import Flask,session#数据库from flask_sqlalchemy import SQLAlchemydb = SQLAlchemy(use_native_unicode='utf8mb4')#实例化SQLAlchemy和使用编码方式from datetime import timedelta,datetime#有的蓝图from .abc import * #abc文件#os随机数import os#创建appdef create_app():app = Flask(__name__,static_url_path='')#app.debug = Falseapp.debug = True#app.secret_key = '123456789'#自定义的session秘钥# 设置配置文件,XXX是数据库名字,是表的上一层数据库名字app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:@127.0.0.1:3306/XXX?charset=utf8mb4"app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Falseapp.config['SQLALCHEMY_ECHO'] = Trueapp.config["SECRET_KEY"] = os.urandom(24)app.config['WTF_CSRF_SECRET_KEY'] = 'NameError'app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1) # 修改缓存时间,秒做单位app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(seconds=1) # 修改回话存活时间,秒做单位#session过期时间app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=6)#查看sqlalchemy执行的原始sql语句app.config['SQLALCHEMY_ECHO'] = True# 注册蓝图app.register_blueprint(abc)# 2. 注册 Flask-SQLAlchemydb.init_app(app)return app#得到了一个app#数据库-Wechatdatabase表-用户class Wechatdatabase(db.Model):__tablename__ = 'wechat_user001'id = db.Column(db.Integer,primary_key=True)openid = db.Column(db.VARCHAR(255))nickname = db.Column(db.VARCHAR(255))sex = db.Column(db.Integer)province = db.Column(db.Text)city = db.Column(db.Text)country = db.Column(db.Text)unionid = db.Column(db.VARCHAR(255))#微信uidpoints = db.Column(db.Integer)#现在拥有的点数success_points = db.Column(db.Integer)#成功充值点数success_times = db.Column(db.Integer)#成功充值次数success_total_fee = db.Column(db.Integer)#成功充值金额orders = db.relationship('Wechatdatabaseorder',backref='nickname2')#这个用户的订单集合#数据库-Wechatdatabaseorder表-订单class Wechatdatabaseorder(db.Model):__tablename__ = 'wechat_user_orders001'id = db.Column(db.Integer,primary_key=True)#订单idopenid = db.Column(db.VARCHAR(255))nickname = db.Column(db.VARCHAR(255))unionid = db.Column(db.VARCHAR(255))out_trade_no = db.Column(db.VARCHAR(255))#订单号order_name = db.Column(db.VARCHAR(255))#订单名state = db.Column(db.VARCHAR(255))#订单状态未付款,已付款creat_time = db.Column(db.DateTime, default=datetime.now)#创建时间time_end = db.Column(db.VARCHAR(14))update_time = db.Column(db.DateTime, default=datetime.now,onupdate=datetime.now)#更新时间total_fee = db.Column(db.Integer)#订单价格spbill_create_ip = db.Column(db.VARCHAR(64))Wechatdatabase_id = db.Column(db.Integer,db.ForeignKey('wechat_user001.id'))#这个系统的下单的人iddb.drop_all()db.create_all() # 创建当前应用中声明的所有模型类对应的数据表,db.drop_all()是删除表
不要的自己注释掉吧
baidu/app/__init__.py
#flaskfrom flask import Flask,session#数据库from flask_sqlalchemy import SQLAlchemydb = SQLAlchemy(use_native_unicode='utf8mb4')#实例化SQLAlchemy和使用编码方式from datetime import timedelta,datetime#有的蓝图from .abc import * #abc文件#os随机数import os#创建appdef create_app():app = Flask(__name__,static_url_path='')#app.debug = Falseapp.debug = True#app.secret_key = '123456789'#自定义的session秘钥# 设置配置文件,XXX是数据库名字,是表的上一层数据库名字app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:@127.0.0.1:3306/XXX?charset=utf8mb4"app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Falseapp.config['SQLALCHEMY_ECHO'] = Trueapp.config["SECRET_KEY"] = os.urandom(24)app.config['WTF_CSRF_SECRET_KEY'] = 'NameError'app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1) # 修改缓存时间,秒做单位app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(seconds=1) # 修改回话存活时间,秒做单位#session过期时间app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=6)#查看sqlalchemy执行的原始sql语句app.config['SQLALCHEMY_ECHO'] = True#微信支付1.程序id: 2.商户号id 3.商户号钥匙 4.回调链接app.config['WEIXIN_APP_ID'] = ''app.config['WEIXIN_APP_MCH_ID'] =''app.config['WEIXIN_APP_MCH_KEY'] =''app.config['WEIXIN_NOTIFY_URL'] ='artchips.art'# 注册蓝图app.register_blueprint(abc)# 2. 注册 Flask-SQLAlchemydb.init_app(app)return app#得到了一个app#数据库-Wechatdatabase表-用户class Wechatdatabase(db.Model):__tablename__ = 'wechat_user001'id = db.Column(db.Integer,primary_key=True)openid = db.Column(db.VARCHAR(255))nickname = db.Column(db.VARCHAR(255))sex = db.Column(db.Integer)province = db.Column(db.Text)city = db.Column(db.Text)country = db.Column(db.Text)unionid = db.Column(db.VARCHAR(255))#微信uidpoints = db.Column(db.Integer)#现在拥有的点数success_points = db.Column(db.Integer)#成功充值点数success_times = db.Column(db.Integer)#成功充值次数success_total_fee = db.Column(db.Integer)#成功充值金额orders = db.relationship('Wechatdatabaseorder',backref='nickname2')#这个用户的订单集合#数据库-Wechatdatabaseorder表-订单class Wechatdatabaseorder(db.Model):__tablename__ = 'wechat_user_orders001'id = db.Column(db.Integer,primary_key=True)#订单idopenid = db.Column(db.VARCHAR(255))nickname = db.Column(db.VARCHAR(255))unionid = db.Column(db.VARCHAR(255))out_trade_no = db.Column(db.VARCHAR(255))#订单号order_name = db.Column(db.VARCHAR(255))#订单名state = db.Column(db.VARCHAR(255))#订单状态未付款,已付款creat_time = db.Column(db.DateTime, default=datetime.now)#创建时间time_end = db.Column(db.VARCHAR(14))update_time = db.Column(db.DateTime, default=datetime.now,onupdate=datetime.now)#更新时间total_fee = db.Column(db.Integer)#订单价格spbill_create_ip = db.Column(db.VARCHAR(64))Wechatdatabase_id = db.Column(db.Integer,db.ForeignKey('wechat_user001.id'))#这个系统的下单的人iddb.drop_all()db.create_all() # 创建当前应用中声明的所有模型类对应的数据表,db.drop_all()是删除表
#蓝图from flask import Blueprintfrom flask import Flask, jsonify, request, url_for,render_template,flash,make_response#真正的数据库from app import db#导入tables类-所有表# from app.abc2 import *import reabc = Blueprint('abc',__name__)#路由1@abc.route("/", methods=['GET','POST'])def hello_flask():print("1")return "1"# 路由2@abc.route("/qqq", methods=['GET','POST'])def qqq():print("1")return "1"