Flask Restful

video 99 API規範html

 1 from flask_restful import Api,Resource
 2 
 3 app = Flask(__name__)
 4 api = Api(app)
 5 
 6 
 7 @app.route('/')
 8 def hello_world():
 9     return 'Hello World!'
10 
11 class LoginView(Resource):
12     def post(self):
13         return {"username":"zhiliao"}
14 api.add_resource(LoginView,'/login/', endpoint="login")

 

 

 

 

video 101 json

 檢查表單:flask

先引入「from flask_restful import Api,Resource,reqparse」api

1 class LoginView(Resource):
2     def post(self):
3         parser = reqparse.RequestParser()
4         parser.add_argument('user', type=int,help='用戶名錯誤')
5         args = parser.parse_args()
6         print(args)
7 
8         return {"username":"zhiliao"}
9 api.add_resource(LoginView,'/login/', endpoint="login")

而後用postman去模擬:restful

由於咱們把user設置爲int型,因此提示了help中的內容。app

 

 

 

video 102 flask-restful標準化返回參數ide

1,最簡單的API寫法:函數

 1 from flask_restful import Api,Resource
 2 app = Flask(__name__)
 3 api = Api(app)
 4 
 5 @app.route('/')
 6 def hello_world():
 7     return 'Hello World!'
 8 
 9 class ArticleView(Resource):
10     def get(self):
11         return {"article":"xxx","title":"aaa"}
12 api.add_resource(ArticleView,'/article/',endpoint = 'article')

2,返回值定義post

 1 from flask_restful import Api,Resource,fields,marshal_with
 2 app = Flask(__name__)
 3 api = Api(app)
 4 
 5 @app.route('/')
 6 def hello_world():
 7     return 'Hello World!'
 8 
 9 class ArticleView(Resource):
10     resource_fields = {
11         'article':fields.String,
12         'title':fields.String,
13     }
14 #marshal_with做用:鏈接resource_fields到視圖。
15 #由於在resource_fields中定義好了返回的參數,即便這個參數沒有值,也會返回,但返回一個None
16 #以上寫法纔是標準API
17     @marshal_with(resource_fields)
18     def get(self):
19         return {"article":"xxx","title":"aaa"}
20 api.add_resource(ArticleView,'/article/',endpoint = 'article')

以上是返回字典,下面講返回模型:spa

3,返回模型

 1 from flask import Flask
 2 from flask_restful import Api,Resource,fields,marshal_with
 3 app = Flask(__name__)
 4 api = Api(app)
 5 
 6 @app.route('/')
 7 def hello_world():
 8     return 'Hello World!'
 9 
10 #創建模型
11 class Article(object):
12     def __init__(self,article,title):
13         self.article = article
14         self.title = title
15 article = Article(article='book1',title='chapter2')
16 
17 class ArticleView(Resource):
18     resource_fields = {
19         'article':fields.String,
20         'title':fields.String,
21     }
22 #marshal_with做用:鏈接resource_fields到視圖。
23 #由於在resource_fields中定義好了返回的參數,即便這個參數沒有值,也會返回,但返回一個None
24 #以上寫法纔是標準API
25     @marshal_with(resource_fields)
26     def get(self):
27         # return {"article":"xxx","title":"aaa"}
28 #Note:這裏返回實例化對象就行。
29         return article
30 api.add_resource(ArticleView,'/article/',endpoint = 'article')

29這裏直接返回一個實例化後的模型就行。效果和以前同樣:

 

video 103 標準化用法(開發過程當中)

 

 

 

video 104 渲染模板

1 from flask_restful import Api,Resource
2 app = Flask(__name__)
3 api = Api(app)
4 
5 class ListView(Resource):
6     def get(self):
7         return render_template("index.html")
8 api.add_resource(ListView,'/list/',endpoint='list')

在restful下渲染html。

結果發現:

那麼,如何解決這個問題呢?

由於restful返回的時候都是倒數第二個json形式,因此出現上面的形式,參考下表:

因此咱們須要修改他爲html代碼,就應該改成第一種html形式。

 1 from flask import Flask,render_template,make_response  2 from flask_restful import Api,Resource
 3 app = Flask(__name__)
 4 api = Api(app)
 5 
 6 #修改返回的類型
 7 @api.representation('text/html')
 8 def output_html(data,code,headers):
 9     print(data)
10     #在representation裝飾的函數中必須返回一個Response對象。
11     # resp = make_response('hello')
12     resp = make_response(data)
13     return resp
14 
15 class ListView(Resource):
16     def get(self):
17         return render_template("index.html")
18 api.add_resource(ListView,'/list/',endpoint='list')

相關文章
相關標籤/搜索