尝试运行 Flask 应用程序时出现错误“函数”对象没有属性“as_view”

一年多以后,我开始编写 Flask 应用程序,我想我忘记了什么。下面的代码会导致错误:


from flask import Flask

from flask import jsonify


from flask_restplus import Resource, Api


from home_iot.config import reader

from download_audio.ydla import download



app = Flask(__name__)


_api = Api(app, catch_all_404s=True, version=0.1,

          title="REST HTTP API's Gateway",

          descrition="REST API gateway")



api_ns = _api.namespace("iot", description="API.")



@api_ns.route("/tcpserver", methods=["GET"])

def advertise_tcpserver():

    ip = reader.get_server_ip()

    return jsonify({"tcpserver": ip})



if __name__ == "__main__":

    app.run(host='127.0.0.1')

错误是:


$ python 应用程序.py


Traceback (most recent call last):

  File "app.py", line 29, in <module>

    @api_ns.route("/tcpserver", methods=["GET"])

  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/namespace.py", line 98, in wrapper

    self.add_resource(cls, *urls, **kwargs)

  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/namespace.py", line 87, in add_resource

    api.register_resource(self, resource, *ns_urls, **kwargs)

  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/api.py", line 264, in register_resource

    self._register_view(self.app, resource, namespace, *urls, **kwargs)

  File "/Users/ciasto/pyenvs/flaskrestplusiot/lib/python2.7/site-packages/flask_restplus/api.py", line 287, in _register_view

    resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args,

AttributeError: 'function' object has no attribute 'as_view'


www说
浏览 185回答 2
2回答

一只萌萌小番薯

希望这可以帮助那些有同样错误但没有找到解决方案的人要完成@v25给出的答案,您必须通过从 flask_restplus 中的 Ressource 类继承来为您的命名空间提供资源。以下示例对我有用环境:Ubuntu 18.04蟒蛇 3.7.1蟒蛇要求:烧瓶==1.1.2flask-restplus==0.13.0工具==0.16.1源代码:iot.pyfrom flask_restplus import Namespace,Resourceapi_ns = Namespace("iot", description="API.")@api_ns.route("/tcpserver")class AdvertiseTcpserver(Resource):&nbsp; &nbsp; def get(self):&nbsp; &nbsp; &nbsp; &nbsp; #TODO return the correct ip value&nbsp; &nbsp; &nbsp; &nbsp; return {"tcpserver": "ip"}app.pyfrom .iot import api_nsfrom flask import Flaskfrom flask_restplus import Apiapp = Flask(__name__)_api = Api(app, catch_all_404s=True, version=0.1,&nbsp; &nbsp; &nbsp; title="REST HTTP API's Gateway",&nbsp; &nbsp; &nbsp; descrition="REST API gateway")_api.add_namespace(api_ns, path='/some/prefix')app.run()测试命令:#!/bin/shwget localhost:5000/some/prefix/tcpserver如果这有帮助,请告诉我。

达令说

不要认为这是用flask_restplus.&nbsp;查看缩放文档。您可能正在寻找类似的东西:iot.pyfrom flask_restplus import Namespaceapi_ns = Namespace("iot", description="API.")@api_ns.route("/tcpserver", methods=["GET"])def advertise_tcpserver():&nbsp; &nbsp; ip = reader.get_server_ip()&nbsp; &nbsp; return jsonify({"tcpserver": ip})然后在你的主app.py中:# other imports&nbsp;from .iot import api_nsapp = Flask(__name__)_api = Api(app, catch_all_404s=True, version=0.1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title="REST HTTP API's Gateway",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; descrition="REST API gateway")_api.add_namespace(api_ns, path='/some/prefix')此外,您似乎正在使用已停产的 Python 2.7。我建议使用虚拟环境或 docker 升级到最新版本,以免弄乱系统的 python。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python