当前位置:网站首页>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')
边栏推荐
- Shenzhi Kalan Temple
- 情人节,我用字符画出了一个对象!
- Plato farm is expected to further expand its ecosystem through elephant swap
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- The response of the database interface is very slow
- Software test interview questions (key points)
- 虚拟机克隆
- STM32 small bug summary
- [uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
- Using ecological power, opengauss breaks through the performance bottleneck
猜你喜欢

Introduction to depth first search (DFS)

IBM3650M4实体机安装VCenter7.0

It's better to be full than delicious; It's better to be drunk than drunk

One book 1201 Fibonacci sequence

我用字符画出了一个谷爱凌!

1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward

Fluent rendering mechanism - GPU thread rendering

借生态力量,openGauss突破性能瓶颈

"PHP Basics" use of integer data

UVM Introduction Experiment 1
随机推荐
Implementation of adding function of background user management display
Demo:st05 find text ID information
ERP生产作业控制 华夏
PHP realizes data interaction with MySQL
1178 questions of Olympiad in informatics -- ranking of grades
[BJDCTF2020]EasySearch 1
Login to homepage function implementation
Redis configuration file download
缓存一致性与内存屏障
Realize SPU management in the background
[NPUCTF2020]ReadlezPHP 1
Risk control and application of informatization project
Shenzhi Kalan Temple
Vcenter7.0 managing esxi7.0 hosts
1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward
开怀一笑
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
JS advanced knowledge - function
2022/7/26 exam summary
Use of elastic box / expansion box (Flex)