当前位置:网站首页>Day4 --- flask blueprint and rest ful
Day4 --- flask blueprint and rest ful
2022-07-27 08:29:00 【qishaoawei】
The blueprint
There are three steps to use the blueprint :
1. Create a blueprint object
2. Operate on this blueprint object , Registered routing , Specify a static folder , Register template filters
3. Register the blueprint object on the application object
Start to create
The realization of the blueprint
Create a app.py File followed by a directory 
app.py In the file
from flask import Flask
from views.user import user_bp # Import blueprint objects from blueprint files
# establish Flask example
app=Flask(__name__)
app.register_blueprint(user_bp) # Registered blueprint
if __name__ == '__main__':
app.run()
In the created views Create under directory user.py file
Be similar to django Subapplications in
# 1 Create a blueprint & 2 Add blueprint route
from flask import Blueprint
# url_prefix # Specify the prefix of all routes under the current blueprint
user_bp=Blueprint('user_bp',__name__,url_prefix='/user')
# Add blueprint route
@user_bp.route('/hello')
def hello():
return 'hello'
Return response 
Flask-RESTful Matching blueprint
In the catalogue user.py In the file
from flask import Blueprint
# url_prefix # Specify the prefix of all routes under the current blueprint
user_bp=Blueprint('user_bp',__name__,url_prefix='/user')
# # First time to know Flask-RESTful
from flask_restful import Api,Resource # Import Api Follow Resource class
# Instantiate a Api object Api Object is used to collect routes
api=Api(user_bp)
# Define a class , Inherited from Resource class , adopt Api To collect instantiated objects
class Users(Resource):
def get(self):
return 'hello2'
## Users Is the defined class name , '/users' Is a custom route
api.add_resource(Users,'/users')
Return response 
Decorator of view class
The basic writing of ornaments
You can specify decorators for a single method
You can also use decorators
# The basic writing of ornaments
def dec1(func):
def warpper(*args,**kwargs):
# The decorator content to be written
print('dec1')
return func(*args,**kwargs)
return warpper
class Users(Resource):
## Only get Methods use the decorator The first one is
method_decorators = {
'get':[dec1]}
### As long as this class is called Just use the decorator The second kind
## method_decorators = [dec1]
def get(self):
return 'hello2'
## Users Is the defined class name , '/users' Is a custom route
api.add_resource(Users,'/users')
边栏推荐
- [MRCTF2020]Ezpop 1
- 2022/7/26 exam summary
- P7 Day1 get to know the flask framework
- Stored procedure test 1 -- first acquaintance of love
- [ciscn2019 southeast China division]web11 1
- [target detection] yolov6 theoretical interpretation + practical test visdrone data set
- All in one 1251 - Fairy Island for medicine (breadth first search)
- List删除集合元素
- 如何卸载--奇安信安全终端管理系统
- Apache SSI remote command execution vulnerability
猜你喜欢
随机推荐
It's better to be full than delicious; It's better to be drunk than drunk
Vertical align cannot align the picture and text vertically
I drew a Gu ailing with characters!
Luogu super Mary game
OPPO 自研大规模知识图谱及其在数智工程中的应用
How does kettle handle text data transfer as' 'instead of null
Realization of specification management and specification option management functions
[NPUCTF2020]ReadlezPHP 1
Introduction, installation and use of netdata performance monitoring tool
QT creator code style plug-in beautifier
Design and development of GUI programming for fixed-point one click query
Risk control and application of informatization project
How to uninstall -- Qianxin secure terminal management system
Login to homepage function implementation
JS basic knowledge - daily learning summary ①
Bandwidth and currency
[netding cup 2020 Qinglong group]areuserialz (buuctf)
Element display mode: block level, inline, inline block, nesting specification, display mode conversion
Data extraction 1
File name wildcard rules for kettle









