函数视图
被url装饰的==>后面处理逻辑的方法就是视图函数
同一函数处理单数和复数 同一函数处理GET/POST等多种方式请求 根据使用习惯,可以一个视图处理1个或多个逻辑
@app. route( '/ware' , methods= [ 'GET' , 'POST' ] )
def ware ( ) :
id = request. args. get( 'id' )
if request. method == 'GET' :
if id is None :
return 'all ware info'
return f'ware info: {id}'
elif request. method == 'POST' :
if id is None :
return 'Ceate a ware'
类视图
标准类视图
类需要继承自Flask.views.View 需要重写dispatch_request方法,否则报错NotImplementedError as_view()意思是将类转成视图函数用,接收一个name参数,定义视图函数名,或者说定义端点名(端点默认取视图函数名) 只能用add_url_rule注册路由
methods: 定义支持的请求方法 dispatch_request: 请求分发 decorators: 自定义装饰器,元组或列表形式的类变量,存放装饰器的顺序与视图函数的顺序一致
from flask import Flask, url_for, redirect, request
from flask. views import View
app = Flask( __name__)
class Ware ( View) :
def dispatch_request ( self) :
return '{}' . format ( request. method)
app. add_url_rule( '/' , view_func= Ware. as_view( 'ware' ) , methods= [ 'POST' ] )
if __name__ == '__main__' :
app. run( debug= True )
class Ware ( View) :
methods = [ 'GET' , 'POST' ]
def get ( self) :
return 'get'
def post ( self) :
return 'post'
def dispatch_request ( self) :
dispatch_pattern = { 'get' : self. get, 'post' : self. post}
method = request. method. lower( )
return dispatch_pattern. get( method) ( )
app. add_url_rule( '/' , view_func= Ware. as_view( 'ware' ) )
def decorater ( func) :
pass
class Ware ( View) :
decorators = ( deco, )
pass
app. add_url_rule( '/' , view_func= Ware. as_view( 'ware' ) )
请求类视图
基于请求方法的类视图, 是根据请求的"method"来执行不同的方法, 若用户是发送的"get"请求, 那么将会执行这个类的"get"方法, 其他的"method"方法类似, 如: “post”, “delete”, “put”
类需要继承Flask.views.MethodView 需要定义对应的请求方法,否则报错Method Not Allowed 注册方式和标准类视图是相同的 自定义装饰器与标准类视图是相同的 不需要重写dispatch_request,仅在需要拓展时重写 不需要通过"request.method=="GET"来判断请求方式,父类通过getattr(self, request.method.lower(), None)获取
class MethodView ( with_metaclass( MethodViewType, View) ) :
"""A class-based view that dispatches request methods to the corresponding
class methods. For example, if you implement a ``get`` method, it will be
used to handle ``GET`` requests. ::
class CounterAPI(MethodView):
def get(self):
return session.get('counter', 0)
def post(self):
session['counter'] = session.get('counter', 0) + 1
return 'OK'
app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))
"""
def dispatch_request ( self, * args, ** kwargs) :
meth = getattr ( self, request. method. lower( ) , None )
if meth is None and request. method == "HEAD" :
meth = getattr ( self, "get" , None )
assert meth is not None , "Unimplemented method %r" % request. method
return meth( * args, ** kwargs)
class UserVIew ( MethodView) :
def get ( self, project_id) :
if project_id is None :
return 'Get All Project'
return f'Get {project_id}'
def post ( self, project_id) :
return f'Post {project_id}'
def put ( self, project_id) :
return f'Put {project_id}'
def delete ( self, project_id) :
return f'Delete {project_id}'
f = UserVIew. as_view( 'user' )
app. add_url_rule( '/projects/<project_id>' , view_func= f, methods= [ 'GET' , 'POST' , 'PUT' , 'DELETE' ] )
app. add_url_rule( '/projects/' , view_func= f, defaults= { 'project_id' : None } , methods= [ 'GET' ] )
传参形式
@app. route( '/<id>' )
def hello ( id ) :
return f'hello {id}'
@app. route( '/' , methods= [ 'GET' ] )
def hello ( ) :
if request. method == 'GET' :
id = request. args. get( 'id' )
return f'hello {id}'
返回值类型
@app. route( '/h' )
def h ( ) :
return 'hello'
@app. route( '/he' )
def he ( ) :
return render_template( 'index.html' )
@app. route( '/hel' )
def hel ( ) :
return jsonify( { 'a' : 1 } )
@app. route( '/hell' )
def hell ( ) :
return 'hello world' , 200 , { 'Locateion' : 'www.baidu.com' }
@app. route( '/hello' )
def hello ( ) :
response = make_response( 'hello' )
response. headers = { 'Location' : 'abc' }
return response