我正在使用Flask Restful
,并且想在根端点上渲染HTML file
:
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
中呈现静态文件?
答案 0 :(得分:0)
看看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
的继承。