我正在用python编写一个带有flask的restful服务器,我正在寻找一种在分离文件中创建嵌套资源的方法。例如,如果我有名为Foo的资源和名为Bar的子资源,而不是写这样的东西:
demo_app.py:
from flask import Flask
from flask_restplus import Api, Resource
app = Flask(__name__)
api = Api(app)
name_space = api.namespace('foo')
@name_space.route('/')
class Foo(Resource):
def get(self):
return 'foo'
@name_space.route('/bar')
class Bar(Resource):
def get(self):
return 'bar'
if __name__ == '__main__':
app.run(debug=True)
我想在不同的文件中写下Foo和Bar:
demo_app.py:
from flask import Flask
from flask_restplus import Api, Resource
app = Flask(__name__)
api = Api(app)
name_space = api.namespace('foo')
if __name__ == '__main__':
app.run(debug=True)
foo.py:
from flask_restful import Resource
from Receiver.demo_app import name_space
@name_space.route('/')
class Foo(Resource):
def get(self):
return 'foo'
bar.py:
from flask_restful import Resource
from Receiver.demo_app import name_space
@name_space.route('/bar')
class Bar(Resource):
def get(self):
return 'bar'
但是当我这样写的时候,我得到了:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
如何在这些类之间建立连接? 有没有其他方法来编写嵌套资源?
答案 0 :(得分:0)
heroku run stopit!
和foo.py
除非将其导入您的其余代码,否则不会生效。您需要添加到bar.py
:
demo_app.py
循环导入的使用与将from . import foo, bar
的引用分割的推荐方法相同;比照http://flask.pocoo.org/docs/0.12/patterns/packages/