<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 106 Flask-Restful標準化返回參數(1) 對于一個視圖函數,你可以指定好一些字段用于返回。以后可以使用ORM模型或者自定義的模型的時候,它會自動的獲取模型中的相應的字段,生成json數據,然后再返回給客戶端,這其中需要導入flask_restfu_.marshal\_with裝飾器。并且需要寫一個字典,來指示需要返回的字段,以及該字段的數據類型。 ```text class ProfileView(Resource): resource_fileds = { 'username':fields.String, 'age':fields.Integer, 'school':fields.String } @narshal_with(resource_fields) def get(self.user_id): user = User.query.get(user_id) return user ``` 在get方法中,返回user的時候,flask\_restful會自動的讀取user模型上的username以及age還有school屬性。組裝成一個json格式的字符串返回給客戶端 ```text from flask import Flask import config from flask_restful import Api,Resource,fields,marshal_with app = Flask(__name__) app.config.from_object(config) api = Api(app) class Article(object): def __init__(self,title,content): self.title = title self.content = content article = Article(title='angle',content='angle') class ArticleView(Resource): # 可以渲染指定字段 resource_fields = { 'title':fields.String, 'content':fields.String, } # restful規范中要求,被定義的參數 # 即使這個參數沒有值,也應該返回,返回一個None回去 @marshal_with(resource_fields) def get(self): # return {"title":"xxx",'content':'xxx'} # 可以返回模型 return article api.add_resource(ArticleView,'/article/',endpoint='article') @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run(debug=True) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看