1
votes

I have a flask application and I'm trying to use flask-restplus and blueprints. Unfortunately my api endpoint always returns The requested URL was not found on the server. even though I can see that it exists in the output of app.url_map. The project is laid out as follows:

- app.py
- api
   - __init__.py
   - resources.py

app.py

from api import api, api_blueprint
from api.resources import EventListResource, EventResource

app = Flask(__name__)
app.register_blueprint(api_blueprint)
db.init_app(flask_app)
app.run()

api/__init__.py

from flask_restplus import Api
from flask import Blueprint

api_blueprint = Blueprint("api_blueprint", __name__, url_prefix='/api')
api = Api(api_blueprint)

api/resources.py

from flask_restplus import Resource
from flask import Blueprint

from . import api, api_blueprint

@api_blueprint.route('/events')
class EventListResource(Resource):
    def get(self):
        "stuff"
        return items

    def post(self):
        "stuff"
        db.session.commit()
        return event, 201

The application starts without issue and I can see that '/api/events' appears in app.url_map so I'm not really sure why the url can't be found. Any help appreciated, thanks!

1

1 Answers

0
votes

Flask-RESTPlus provides a way to use almost the same pattern as Flask’s blueprint. The main idea is to split your app into reusable namespaces.

You can do it this way:

app.py

from flask_restplus import Api
from api import api_namespace

app = Flask(__name__)
api = Api(app)
db.init_app(flask_app)

from api import api_namespace
api.add_namespace(api_namespace, path='/api')
app.run()

api/init.py

from flask_restplus import Namespace

api_namespace = Namespace('api_namespace')

api/resources.py

from flask_restplus import Resource

from api import api_namespace

@api_namespace.route('/events')
class EventListResource(Resource):
    def get(self):
        "stuff"
        return items

    def post(self):
        "stuff"
        db.session.commit()

Here is the link to documentation: https://flask-restplus.readthedocs.io/en/stable/scaling.html