Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this custom middleware modifying response headers?
Consider this Django middleware that adds a custom header to every response. What will be the value of the
X-Custom-Header in the HTTP response?Django
class CustomHeaderMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) response["X-Custom-Header"] = "MiddlewareActive" return response
Attempts:
2 left
💡 Hint
Think about how the middleware modifies the response object before returning it.
✗ Incorrect
The middleware adds the header
X-Custom-Header with value MiddlewareActive to every response before returning it.❓ lifecycle
intermediate1:30remaining
At which point in the request lifecycle is the
process_view middleware method called?In Django middleware, when is the
process_view method executed?Attempts:
2 left
💡 Hint
Think about when Django knows which view will handle the request.
✗ Incorrect
process_view runs after Django resolves the URL to a view but before the view function is executed.📝 Syntax
advanced2:30remaining
Which middleware class definition correctly implements
__init__ and __call__ methods?Select the option that correctly defines a Django middleware class with proper
__init__ and __call__ methods.Attempts:
2 left
💡 Hint
Remember the middleware class must accept get_response in __init__ and request in __call__.
✗ Incorrect
Option D correctly defines __init__ with get_response and __call__ with request, calling get_response(request). Others have wrong method signatures.
🔧 Debug
advanced2:00remaining
Why does this middleware raise an AttributeError?
Given this middleware code, why does it raise an AttributeError when processing a request?
Django
class FaultyMiddleware: def __init__(self, get_response): pass def __call__(self, request): response = self.get_response(request) return response
Attempts:
2 left
💡 Hint
Check if all attributes used in __call__ are properly set in __init__.
✗ Incorrect
The middleware does not assign get_response to self.get_response in __init__, so self.get_response is undefined in __call__, causing AttributeError.
❓ state_output
expert3:00remaining
What is the final value of
request.custom_flag after this middleware stack runs?Two middlewares modify
request.custom_flag. What is its value after both run?Django
class MiddlewareOne: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): request.custom_flag = True response = self.get_response(request) return response class MiddlewareTwo: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if hasattr(request, 'custom_flag') and request.custom_flag: request.custom_flag = False response = self.get_response(request) return response # Assume MiddlewareOne is listed before MiddlewareTwo in MIDDLEWARE setting
Attempts:
2 left
💡 Hint
Remember middleware runs in order on request, reverse order on response.
✗ Incorrect
MiddlewareOne sets custom_flag to True first. MiddlewareTwo then sees it True and sets it to False. So final value after both is False.