Home > Blockchain >  AttributeError: 'Flask' object has no attribute 'get
AttributeError: 'Flask' object has no attribute 'get

Time:12-18

I'm getting the following error while running the below code

File "D:\Inteliji Projects\Python Flask\whatsgrouprestapi\src\app.py", line 13, in create_app @app.get("/") AttributeError: 'Flask' object has no attribute 'get

from flask import Flask

def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)

    if test_config is None:
        app.config.from_mapping(
            SECRET_KEY="dev"
        )
    else:
        app.config.from_mapping(test_config)

    @app.get("/")
    def index():
        return "Hello World"

    @app.get("/hello")
    def say_hello():
        return {"message" : "Hellos World"}

    return app

What is wrong with the code

Updated

After i changed my code like this it is working fine I don't know what is the issue

def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)

    if test_config is None:
        app.config.from_mapping(SECRET_KEY=os.environ.get("SECRET_KEY"),)
    else:
        app.config.from_mapping(test_config)

    @app.route('/', methods=["GET"])
    def index():
        return "Hello World"

    @app.route('/hello', methods=["GET"])
    def say_hello():
        return {"message" : "Hellos World"}

    return app

CodePudding user response:

The .get shorthand has been added in Flask version 2.0 and is not available in older versions. See the documentation's Changelog: https://flask.palletsprojects.com/en/2.2.x/api/#flask.Flask.get.

  • Related