Flask - Middleware and ExtensionsWhich of the following code snippets correctly shows the extension initialization pattern in Flask?Adef create_app(): app = Flask(__name__) db = SQLAlchemy() return appBdef create_app(): app = Flask(__name__) db = SQLAlchemy(app) return appCdb = SQLAlchemy(app) def create_app(): app = Flask(__name__) return appDdb = SQLAlchemy() def create_app(): app = Flask(__name__) db.init_app(app) return appCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify correct extension creationThe extension instance should be created outside the factory without app argument.Step 2: Check init_app usage inside factoryThe extension is initialized with the app inside the factory using init_app(app).Final Answer:db = SQLAlchemy() outside factory, then db.init_app(app) inside factory. -> Option DQuick 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:MISTAKESPassing app to extension constructor outside factoryNot calling init_app at allCreating extension inside factory without init_app
Master "Middleware and Extensions" in Flask9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Flask Quizzes Background Tasks - Periodic tasks with Celery Beat - Quiz 14medium Flask Ecosystem and Patterns - Service layer pattern - Quiz 11easy Middleware and Extensions - WSGI middleware concept - Quiz 14medium Middleware and Extensions - Before_request as middleware alternative - Quiz 11easy Security Best Practices - XSS prevention in templates - Quiz 6medium Security Best Practices - Password storage best practices - Quiz 7medium Security Best Practices - Secure headers configuration - Quiz 6medium Security Best Practices - Input sanitization - Quiz 11easy WebSocket and Real-Time - Room-based messaging - Quiz 4medium WebSocket and Real-Time - Why real-time matters - Quiz 10hard