FlaskAPI - AttributeError: module 'app.routers.api' has no attribute 'register'

98 views Asked by At

4

I am trying to setup an app using Flas API but keep getting this error which I can't make sense of. My api.py file is as follows:

from flask import Blueprint, request
from app.models import User
from app.db import get_db


bp = Blueprint('api', __name__, url_prefix = '/api')

@bp.route('/users', methods = ['POST'])
def signup():
    data = request.get_json()
    print(data)
    return ''

routes/init.py

from .home import bp as home
from .dashboard import bp as dashboard
from .api import bp as api

and where the error is coming from: app/init.py

from flask import Flask
from app.routes import home, dashboard, api
from app.db import init_db
from app.utils import filters

def create_app(test_config=None):
    # set up app config
    app = Flask(__name__, static_url_path='/')
    app.url_map.strict_slashes = False
    app.config.from_mapping(
        SECRET_KEY='super_secret_key'
    )
    
    @app.route('/hello')
    def hello():
        return 'Hello, World!'
    
    # register routes
    app.register_blueprint(home)
    app.register_blueprint(dashboard)
    app.register_blueprint(api) // <-- this is what is saying the error is from 
    
    # Jinja filters
    app.jinja_env.filters['format_url'] = filters.format_url
    app.jinja_env.filters['format_date'] = filters.format_date
    app.jinja_env.filters['format_plural'] = filters.format_plural
    
    init_db(app)

    return app

But I keep getting the following error:

File "python-newsfeed-uaw/app/__init__.py", line 21, in create_app
    app.register_blueprint(api)
  File "python-newsfeed-uaw/venv/lib/python3.9/site-packages/flask/scaffold.py", line 51, in wrapper_func
    return f(self, *args, **kwargs)
  File "python-newsfeed-uaw/venv/lib/python3.9/site-packages/flask/app.py", line 997, in register_blueprint
    blueprint.register(self, options)
AttributeError: module 'app.routes.api' has no attribute 'register'
0

There are 0 answers