gpt4 book ai didi

python - 属性错误: class Home has no attribute 'as_view'

转载 作者:行者123 更新时间:2023-12-01 08:54:08 28 4
gpt4 key购买 nike

我正在使用 Flask Restful,并且我想在根端点上渲染 HTML 文件:

from flask_restful import Resource, Api
app = Flask( __name__, static_url_path = '', static_folder = "docs" )
api = Api(app, catch_all_404s=True)
class Home():
def get(self):
return app.send_static_file( 'index.html' )
api.add_resource(Home, '/')

当我运行此代码时,出现此错误:

    api.add_resource(Home, '/')
File "/home/vagrant/.local/lib/python2.7/site-packages/flask_restful/__init__.py", line 404, in add_resource
self._register_view(self.app, resource, *urls, **kwargs)
File "/home/vagrant/.local/lib/python2.7/site-packages/flask_restful/__init__.py", line 444, in _register_view
resource_func = self.output(resource.as_view(endpoint, *resource_class_args,
AttributeError: class Home has no attribute 'as_view'

如何在Flask Restful中渲染静态文件?

最佳答案

看看Flask Restful Documentation ;作为示例,您可以在文档中找到以下内容:

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

todos = {}

class TodoSimple(Resource):
def get(self, todo_id):
return {todo_id: todos[todo_id]}

def put(self, todo_id):
todos[todo_id] = request.form['data']
return {todo_id: todos[todo_id]}

api.add_resource(TodoSimple, '/<string:todo_id>')

if __name__ == '__main__':
app.run(debug=True)

正如您在上面所看到的,您肯定缺少的一件事是来自 Resource 的继承。

关于python - 属性错误: class Home has no attribute 'as_view' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52887871/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com