Middleware in Flask is a piece of code that runs before or after the main request handler. It can modify requests or responses, like a helpful assistant checking or changing things as they pass through.
Click to reveal answer
intermediate
How do you create custom middleware in Flask?
You create a class with a __call__ method that takes the WSGI environment and start_response function. Inside, you can add your code before or after calling the next app in the chain.
Click to reveal answer
intermediate
What does the __call__ method do in Flask middleware?
The __call__ method makes the middleware class behave like a function. Flask calls it with request details, letting your middleware process or change the request or response.
Click to reveal answer
beginner
Why use middleware in a Flask app?
Middleware helps add features like logging, authentication, or modifying requests without changing your main app code. It keeps your app clean and organized.
Click to reveal answer
intermediate
How do you add your custom middleware to a Flask app?
Wrap your Flask app with your middleware class when creating the app. For example, app.wsgi_app = YourMiddleware(app.wsgi_app). This tells Flask to use your middleware.
Click to reveal answer
What method must a Flask middleware class implement to be callable?
Ahandle
B__call__
Crun
D__init__
✗ Incorrect
The __call__ method allows the middleware class to be called like a function by Flask.
Where do you insert your middleware in a Flask app?
AWrap the app's wsgi_app attribute
BInside the route functions
CIn the app.config dictionary
DIn the templates folder
✗ Incorrect
Middleware wraps the app's wsgi_app attribute to intercept requests and responses.
What can middleware modify in Flask?
AHTML templates
BDatabase schema
CRequests and responses
DStatic files
✗ Incorrect
Middleware can modify the incoming requests or outgoing responses.
Which of these is NOT a typical use of middleware?
AModifying headers
BAuthenticating users
CLogging requests
DRendering HTML templates
✗ Incorrect
Rendering HTML templates is done in route handlers, not middleware.
What arguments does the __call__ method in Flask middleware receive?
Aenviron and start_response
Brequest and response
Capp and config
Dself and app
✗ Incorrect
The __call__ method receives the WSGI environ dictionary and start_response function.
Explain how to create and add custom middleware to a Flask app.
Think about how middleware acts like a filter around your app.
You got /4 concepts.
Describe three common uses of middleware in Flask applications.
Middleware helps with tasks before or after your main app runs.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of creating custom middleware in a Flask application?
easy
A. To run code before and after each request is processed by Flask
B. To define new routes and views in the Flask app
C. To replace Flask's built-in request handling completely
D. To create database models for the application
Solution
Step 1: Understand middleware role
Middleware runs code around request processing, before and after Flask handles requests.
Step 2: Compare options
Only To run code before and after each request is processed by Flask describes this behavior. Other options describe unrelated tasks.
Final Answer:
To run code before and after each request is processed by Flask -> Option A
Quick Check:
Middleware purpose = run code around requests [OK]
Hint: Middleware wraps requests to add extra processing [OK]
Common Mistakes:
Confusing middleware with route creation
Thinking middleware replaces Flask core
Mixing middleware with database models
2. Which of the following is the correct way to define a custom middleware class in Flask?
easy
A. class MyMiddleware:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
return self.app(environ, start_response)
B. def MyMiddleware(app):
return app
C. class MyMiddleware:
def handle_request(self):
pass
D. class MyMiddleware:
def __init__(self):
pass
Solution
Step 1: Check middleware class structure
Middleware must accept the app in __init__ and implement __call__ with environ and start_response.
Step 2: Validate options
Only class MyMiddleware:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
return self.app(environ, start_response) matches this pattern correctly. Others miss __call__ or parameters.
Final Answer:
Correct middleware class with __init__ and __call__ methods -> Option A
Quick Check:
Middleware class needs __call__ method [OK]
Hint: Middleware class must implement __call__(environ, start_response) [OK]
Common Mistakes:
Omitting __call__ method
Not passing app to __init__
Using function instead of class
3. Given this middleware code, what will be printed when a request is made?