Bird
0
0

Which of the following code snippets correctly shows the extension initialization pattern in Flask?

easy📝 Syntax Q3 of 15
Flask - Middleware and Extensions
Which of the following code snippets correctly shows the extension initialization pattern in Flask?
Adef create_app(): app = Flask(__name__) db = SQLAlchemy() return app
Bdef create_app(): app = Flask(__name__) db = SQLAlchemy(app) return app
Cdb = SQLAlchemy(app) def create_app(): app = Flask(__name__) return app
Ddb = SQLAlchemy() def create_app(): app = Flask(__name__) db.init_app(app) return app
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct extension creation

    The extension instance should be created outside the factory without app argument.
  2. Step 2: Check init_app usage inside factory

    The extension is initialized with the app inside the factory using init_app(app).
  3. Final Answer:

    db = SQLAlchemy() outside factory, then db.init_app(app) inside factory. -> Option D
  4. Quick Check:

    Extension created outside + init_app inside factory = db = SQLAlchemy() def create_app(): app = Flask(__name__) db.init_app(app) return app [OK]
Quick Trick: Create extension globally, init inside factory with init_app [OK]
Common Mistakes:
MISTAKES
  • Passing app to extension constructor outside factory
  • Not calling init_app at all
  • Creating extension inside factory without init_app

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes