简体   繁体   English

尝试运行 flask 应用程序时出现错误“功能”object 没有属性“as_view”

[英]getting error 'function' object has no attribute 'as_view' while trying to run flask app

I started writing flask app after a long time more than a year, guess I have forgot something.经过一年多的时间,我开始写 flask 应用程序,我想我忘记了什么。 This below code results in an error:下面的代码会导致错误:

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')

Error is:错误是:

$ python app.py $ python app.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'

Don't think that's the correct way to define the namespace with flask_restplus .不要认为这是用flask_restplus定义命名空间的正确方法。 Have a look at scaling docs .看看缩放文档

You're probably looking for something like:您可能正在寻找类似的东西:

iot.py物联网

from flask_restplus import Namespace

api_ns = Namespace("iot", description="API.")

@api_ns.route("/tcpserver", methods=["GET"])
def advertise_tcpserver():
    ip = reader.get_server_ip()
    return jsonify({"tcpserver": ip})

Then in your main app.py :然后在你的主app.py 中

# other imports 

from .iot import api_ns

app = Flask(__name__)

_api = Api(app, catch_all_404s=True, version=0.1,
          title="REST HTTP API's Gateway",
          descrition="REST API gateway")

_api.add_namespace(api_ns, path='/some/prefix')

Also you appear to be using Python 2.7 which has been discontinued.此外,您似乎正在使用已停产的 Python 2.7。 I'd suggest upgrading to the latest version, using either a virutal environment or docker so as not to mess with your system's python.我建议使用虚拟环境或 docker 升级到最新版本,以免弄乱系统的 python。

Hope this can helps those who have this same error and have not found the solution希望这可以帮助那些有同样错误但没有找到解决方案的人

To complete the answer given by @v25 you must provide ressources to your namespace by inheriting from Ressource class in flask_restplus.要完成@v25给出的答案,您必须通过从 flask_restplus 中的 Ressource class 继承来为您的命名空间提供资源。

The following example works for me以下示例适用于我

Environment:环境:

  • ubuntu 18.04 ubuntu 18.04
  • python 3.7.1 python 3.7.1

python requirements: python 要求:

  • flask==1.1.2烧瓶==1.1.2
  • flask-restplus==0.13.0烧瓶休息加==0.13.0
  • werkzeug==0.16.1 werkzeug==0.16.1

Source code: iot.py源代码:iot.py

from flask_restplus import Namespace,Resource

api_ns = Namespace("iot", description="API.")

@api_ns.route("/tcpserver")
class AdvertiseTcpserver(Resource):
    def get(self):
        #TODO return the correct ip value
        return {"tcpserver": "ip"}

app.py应用程序.py

from .iot import api_ns
from flask import Flask
from flask_restplus import Api

app = Flask(__name__)

_api = Api(app, catch_all_404s=True, version=0.1,
      title="REST HTTP API's Gateway",
      descrition="REST API gateway")

_api.add_namespace(api_ns, path='/some/prefix')

app.run()

Test command:测试命令:

#!/bin/sh
wget localhost:5000/some/prefix/tcpserver

Please let me know if this helped.请让我知道这是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么显示此错误“功能”对象没有属性“ as_view” - Why show this Error 'function' object has no attribute 'as_view' “函数”对象没有属性“as_view” - 'function' object has no attribute 'as_view' AttributeError:“函数”对象没有属性“as_view” - AttributeError: 'function' object has no attribute 'as_view' AttributeError: 'function' object 在 urls.py 中没有属性 'as_view' - AttributeError: 'function' object has no attribute 'as_view',in urls.py Django“ AttributeError:&#39;function&#39;对象没有属性&#39;as_view&#39;” - Django “AttributeError: 'function' object has no attribute 'as_view'” AttributeError: 'function' object 没有属性 'as_view'。 怎么了? - AttributeError: 'function' object has no attribute 'as_view'. What's wrong? Flask-restful AttributeError:类型 object 'Project' 没有属性 'as_view' - Flask-restful AttributeError: type object 'Project' has no attribute 'as_view' 关于Flask中的as_view功能 - About as_view function in Flask 基于类的视图“没有属性.as_view()”错误 - Class-based view “has no attribute .as_view()” error Django:AttributeError:&#39;function&#39;对象在urls.py中没有显示属性&#39;as_view&#39; - Django: AttributeError: 'function' object has no attribute 'as_view' showing in urls.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM