当前位置:网站首页>Flask project configuration
Flask project configuration
2022-07-27 08:25:00 【Csdn__ F】
1. Create project files , Configure virtual environment
python Virtual environment construction
2. establish app.py file
from create_app import create_app
from settings.config import DevConfig
app=create_app(DevConfig) # Here is the flask The core object
function
function
3. establish create_app.py file
from flask import Flask
from flask_cors import CORS
# Factory function
def create_app(config): # config by config.py Configuration items in
# flask The core object
flask_app = Flask(__name__) # Instantiation flask object
# Cross domain
CORS(flask_app)
# Load configuration items
# Registered blueprint
# binding db Database
return flask_app
4. Configuration item
4.1 establish settings Folder , establish config.py file
# Custom base class , You can reuse
class DefaultConfig:
SECRET_KEY='fskfjkdgdshljgdfdsfs' # Custom key
SQLALCHEMY_DATABASE_URI=""
SQLALCHEMY_TRACK_MODIFICATIONS=False
SQLALCHEMY_ECHO=True
JSON_AS_ASCII = False
# Inherit the base class above , Change the database configuration items
class DevConfig(DefaultConfig):
# user name : password @host:port: database
# root:[email protected]:3306/p7_day05
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:[email protected]:3306/p7_day05'
4.2 Mount to create_app.py In file
from flask import Flask
from flask_cors import CORS
# Factory function
def create_app(config): # config by config.py Configuration items in
# flask The core object
flask_app = Flask(__name__) # Instantiation flask object
# Cross domain
CORS(flask_app)
# Load configuration items
flask_app.config.from_object(config)
# Registered blueprint
# binding db Database
return flask_app
5. The blueprint
5.1 establish views Folders are used to create blueprints
establish orders.py file
from flask import Blueprint
from flask_restful import Api, Resource
# url_prefix='/orders' Prefix all routes for the current blueprint
order_bp = Blueprint("order_bp", __name__, url_prefix='/orders') # Instantiate blueprint objects
api = Api(order_bp) # Instantiate route collector
# We will increase, delete, and change inspections
# Create class view
class OrderView(Resource):
def post(self):
return "111"
# Add route
api.add_resource(KindView, '/order') # The full address is /orders/order
5.2 After creating the blueprint, you need to go to create_app.py Registration blueprint in file
from flask import Flask
from flask_cors import CORS
from views.orders import *
def create_app(config):
flask_app = Flask(__name__) # Instantiation flask object
# Cross domain
CORS(flask_app)
# Load configuration items
flask_app.config.from_object(config)
CORS(flask_app)
# Registered blueprint
flask_app.register_blueprint(order_bp) # order_bp Is the instantiated blueprint object
# binding db Database
return flask_app
6. Model
6.1 establish models The folder is used to put the model , Create... In the folder model.py file
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy() # Instantiate database connection object
# Create model classes
class OrdersModel(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="ID")
name = db.Column(db.String(32), comment=" name ")
6.2 To create_app.py Binding model classes in
from flask import Flask
from models.model import db
from views.orders import *
def create_app(config):
flask_app = Flask(__name__) # Instantiation flask object
# Cross domain
CORS(flask_app)
# Load configuration items
flask_app.config.from_object(config)
CORS(flask_app)
# Registered blueprint
flask_app.register_blueprint(order_bp) # order_bp Is the instantiated blueprint object
# First, there should be the database in the configuration item , In the binding db
# binding db Database
db.init_app(flask_app)
return flask_app
7. Create in project root manage.py Used to migrate
from app import app
# Import migration classes and migration command classes
from flask_migrate import MigrateCommand, Migrate
# Import management class
from flask_script import Manager
from models.model import db
manage = Manager(app) # Instantiate the command line management object
migrate = Migrate(app, db) # Instantiate the migration execution class
manage.add_command("db", MigrateCommand) # Add a set of commands to the command line management object
if __name__ == '__main__':
manage.run()
00 Execute the migration command
python run.py db init initialization , It only needs to be initialized once
python run.py db migrate Generate migration file
python run.py db upgrade transfer
边栏推荐
- Dormitory access control system made by imitating the boss (III)
- 【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
- [target detection] yolov6 theoretical interpretation + practical test visdrone data set
- XxE & XML vulnerability
- Solve the problem of slow batch insertion of MySQL JDBC data
- Hundreds of people participated. What are these people talking about in the opengauss open source community?
- [geek challenge 2019] finalsql 1
- One book 1201 Fibonacci sequence
- Netdata 性能监测工具介绍、安装、使用
- 帮忙发几个招聘,有兴趣可以看看
猜你喜欢

虚拟机克隆
![[target detection] yolov6 theoretical interpretation + practical test visdrone data set](/img/ad/78835eea4decc15e0981e6561b875f.png)
[target detection] yolov6 theoretical interpretation + practical test visdrone data set
![[MRCTF2020]PYWebsite 1](/img/d4/2d9cd06abd7188add668cde77d3075.png)
[MRCTF2020]PYWebsite 1

A quick overview of transformer quantitative papers in emnlp 2020
![[geek challenge 2019] finalsql 1](/img/a7/857d47639fcb38e0055a2444206b8c.png)
[geek challenge 2019] finalsql 1

Attack and defense world MFW

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

Bandwidth and currency
![[netding cup 2020 rosefinch group]nmap 1 two solutions](/img/fa/b1349cb42b5768b7510217239ba73a.png)
[netding cup 2020 rosefinch group]nmap 1 two solutions

百人参与,openGauss开源社区这群人都在讨论什么?
随机推荐
Weekly learning summary
Set set
How to log in multiple wechat on the computer
Luogu super Mary game
Data extraction 2
Lua iterator
Dirsearch[directory scanning tool]
说透缓存一致性与内存屏障
containerd拉取私库镜像失败(kubelet)
Solve the problem of slow batch insertion of MySQL JDBC data
CommonTitleBar hide left right
Shenzhi Kalan Temple
[applet] how to get wechat applet code upload key?
Teach you to build a nail warning robot hand in hand
[BJDCTF2020]EasySearch 1
Flutter 渲染机制——GPU线程渲染
JS rotation chart
Qt Creator代码风格插件Beautifier
借生态力量,openGauss突破性能瓶颈
STM32 small bug summary
