0
0
Djangoframework~20 mins

Async middleware in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this async middleware logging example?
Consider this Django async middleware that logs request and response types. What will be printed in the console when a request is processed?
Django
import asyncio
from django.utils.deprecation import MiddlewareMixin

class AsyncLoggingMiddleware:
    async def __call__(self, request):
        print(f"Request type: {type(request)}")
        response = await self.get_response(request)
        print(f"Response type: {type(response)}")
        return response
ARequest type: <class 'asyncio.Future'>\nResponse type: <class 'asyncio.Future'>
BRequest type: <class 'django.http.HttpRequest'>\nResponse type: <class 'asyncio.coroutine'>
CRequest type: <class 'django.http.HttpRequest'>\nResponse type: <class 'django.http.HttpResponse'>
DRequest type: <class 'django.http.HttpResponse'>\nResponse type: <class 'django.http.HttpResponse'>
Attempts:
2 left
💡 Hint
Remember that Django passes HttpRequest objects to middleware and expects HttpResponse objects back.
📝 Syntax
intermediate
2:00remaining
Which async middleware definition is syntactically correct in Django?
Select the option that correctly defines an async middleware class in Django 4+.
A
class MyMiddleware(MiddlewareMixin):
    async def __call__(self, request):
        response = self.get_response(request)
        return response
B
class MyMiddleware:
    async def __call__(self, request):
        response = await self.get_response(request)
        return response
C
class MyMiddleware:
    def __call__(self, request):
        response = await self.get_response(request)
        return response
D
class MyMiddleware:
    async def process_request(self, request):
        response = await self.get_response(request)
        return response
Attempts:
2 left
💡 Hint
Async middleware must define an async __call__ method and await the response.
🔧 Debug
advanced
2:00remaining
Why does this async middleware cause a runtime error?
Given this middleware code, what is the cause of the runtime error when processing requests asynchronously? class FaultyMiddleware: def __call__(self, request): response = self.get_response(request) return response
AThe __call__ method is not async but calls an async get_response without awaiting, causing a coroutine not awaited error.
BThe middleware class is missing the MiddlewareMixin base class causing attribute errors.
CThe get_response method is not defined in the middleware causing an AttributeError.
DThe middleware uses sync __call__ but the request object is async-only causing a TypeError.
Attempts:
2 left
💡 Hint
Check if async functions are awaited properly in sync methods.
state_output
advanced
2:00remaining
What is the final value of request.state['count'] after this async middleware chain?
Two async middlewares increment a counter in request.state. What is the final count after processing a request? Middleware1: async def __call__(self, request): request.state['count'] = request.state.get('count', 0) + 1 response = await self.get_response(request) return response Middleware2: async def __call__(self, request): request.state['count'] = request.state.get('count', 0) + 1 response = await self.get_response(request) return response
A2
B1
CRaises KeyError because request.state is not initialized
D0
Attempts:
2 left
💡 Hint
Each middleware increments the count once before passing the request on.
🧠 Conceptual
expert
2:00remaining
Which statement best describes async middleware behavior in Django 4+?
Choose the most accurate statement about async middleware in Django 4 and later versions.
AAsync middleware must inherit from MiddlewareMixin to work correctly with async views.
BAsync middleware can only call async views; sync views cause runtime errors if called from async middleware.
CAsync middleware cannot modify the request object because it is immutable during async processing.
DAsync middleware must define an async __call__ method and can call both sync and async views seamlessly by awaiting get_response.
Attempts:
2 left
💡 Hint
Think about how Django handles sync and async views with async middleware.