Reputation: 2981
I am working with my first flask-RESTplus application and running into issues. Here is how my project is structured:
proj/
- endpoints/
- __init__.py
- example1.py
- app.py
This is what I have in my init.py:
from flask import Blueprint
from flask_restplus import Api
blueprint1 = Blueprint('api', __name__)
api = Api(blueprint1,version='1.0', title='Sample API',
description='A sample API',
)
ns = api.namespace('todos', description='todo')
My example1.py has below code:
from flask import Flask , request, Blueprint
from flask_restplus import Api, Resource, fields, Namespace
from endpoints import ns
todo = ns.model('Todo', {
'task': fields.String(required=True, description='The task details')
})
@ns.route('/api_route1')
class Todo(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@ns.doc(parser=parser)
@ns.expect(todo)
#@ns.marshal_list_with(todo)
def post(self):
#processing code
return message
from app.py, this is how I try to invoke the app:
from flask import Flask , request, Blueprint
from flask_restplus import Api, Resource, fields
from werkzeug.middleware.proxy_fix import ProxyFix
from endpoints import api
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.register_blueprint(api)
if __name__ == '__main__':
app.run(debug=True)
When I run my app.py, I get below error:
Traceback (most recent call last):
File "Python38-32\lib\site-packages\flask_restplus\api.py", line 215, in __getattr__
return getattr(self.default_namespace, name)
AttributeError: 'Namespace' object has no attribute 'register'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 50, in <module>
app.register_blueprint(api)
File "Python38-32\lib\site-packages\flask\app.py", line 98, in wrapper_func
return f(self, *args, **kwargs)
File "Python38-32\lib\site-packages\flask\app.py", line 1167, in register_blueprint
blueprint.register(self, options, first_registration)
File "Python38-32\lib\site-packages\flask_restplus\api.py", line 217, in __getattr__
raise AttributeError('Api does not have {0} attribute'.format(name))
AttributeError: Api does not have register attribute
I have been going through documentation at this link but unable to get this working. Any help would be appreciated!
Note: I have python 3.8 and flask-restplus 0.11.0
Upvotes: 3
Views: 3608
Reputation: 668
The problem is you're registering the Api
object with the app instead of the blueprint. In the documentation you mentioned, it shown that the blueprint
is import as api
from flask import Flask
from apis import blueprint as api
app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api/1')
app.run(debug=True)
so if you do
from endpoints import blueprint1 as api
app = Flask(__name__)
app.register_blueprint(api)
it will work properly!
Upvotes: 5