Home > OS >  why is the blueprint route not found with a registered url_prefix
why is the blueprint route not found with a registered url_prefix

Time:10-24

I have a file that registers a blueprint and runs the app api/v1/app.py:

#!/usr/bin/python3
"""returns api status code"""

from api.v1.views import app_views
from flask import Flask
from models import storage
from os import getenv

app = Flask(__name__)
app.register_blueprint(app_views, url_prefix="/api/v1")


@app.teardown_appcontext
def close_session(session):
    """close a session"""
    storage.close()


if __name__ == '__main__':
    host = getenv('HBNB_API_HOST') if getenv('HBNB_API_HOST') else '0.0.0.0'
    port = getenv('HBNB_API_PORT') if getenv('HBNB_API_PORT') else 5000
    app.run(port=port, host=host, threaded=True)

a file that creates a blueprint api/v1/views/__init__.py:

#!/usr/bin/python3
"""create a blueprint"""

from flask import Blueprint

app_views = Blueprint("app_views", __name__)

and a file that creates a blueprint route api/v1/views/index.py:

#!/usr/bin/python3
"""returns api status code"""

from flask import jsonify
from api.v1.views import app_views


@app_views.route('/status')
def status():
    """return ok response"""
    return jsonify({"status": "OK"})

i registered the blueprint with the url_prefix /api/v1, but when i try to access http://0.0.0.0:5000/api/v1/status, I get 404.

So what am I missing here ?

CodePudding user response:

I am not sure how to solve it.

However, try to print(or logging) app.url_map and the results

Something like below

Map(...,
 <Rule '/api/v1/status' -> app_view.status>,
...)

printed? if not, please share the result for further checks..

CodePudding user response:

You defined the prefix correctly. The problem here is because the file api/v1/views/index.py was never interpreted. If you add import api/v1/views/index.py in the file api/v1/app.py it will probably work

  • Related