I writing a flask, trying to organize it by using Blueprint with Namespace, following this tutorial
I had faced some problem, and had look around internet and had review solution in 1 and 2. The first one is not relevant to what I doing, and the second one the solution just doesn't fix my problem.
Here are my code:
project/project.py
from flask import Flask, jsonify, url_for
from .apis.apis import api
app = Flask(__name__)
app.register_blueprint(api, url_prefix="/api")
project/apis/apis.py
from flask import Blueprint
from .user.authentication import auth
from flask_restplus import Api, apidoc, Resource
blueprint = Blueprint("api", __name__)
api = Api(blueprint, doc='/docs', ui=False)
api.add_namespace(auth, path="/auth") #Keep getting error at this line
project/apis/user/authentication.py
from flask_restplus import Namespace
auth = Namespace('auth', description='Authentication')
@auth.route("/test")
def authentication():
return "test"
Stack Trace
Traceback (most recent call last):
File "/home/gaara/Python/Flask-Api/project/__init__.py", line 1, in <module>
from .project import app
File "/home/gaara/Python/Flask-Api/project/project.py", line 3, in <module>
from .apis.apis import api
File "/home/gaara/Python/Flask-Api/project/apis/apis.py", line 13, in <module>
api.add_namespace(auth, path="/auth")
File "/home/gaara/Python/Flask-Api/venv/lib/python3.6/site-packages/flask_restplus/api.py", line 413, in add_namespace
self.register_resource(ns, resource, *self.ns_urls(ns, urls), **kwargs)
File "/home/gaara/Python/Flask-Api/venv/lib/python3.6/site-packages/flask_restplus/api.py", line 255, in register_resource
self._register_view(self.app, resource, *urls, **kwargs)
File "/home/gaara/Python/Flask-Api/venv/lib/python3.6/site-packages/flask_restplus/api.py", line 276, in _register_view
resource_func = self.output(resource.as_view(endpoint, self, *resource_class_args,
AttributeError: 'function' object has no attribute 'as_view'
I am not sure why I keep getting this error, had try few approach, include put apis.py all in __init__.py and change the import, but always getting the same error.
What I wish is to code api in an organize way, and when go to localhost:5000/api/auth/test
it will output me test