Challenge - 5 Problems
Async Django Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this async Django view?
Consider this async Django view. What will the client receive as a response body?
Django
from django.http import JsonResponse import asyncio async def my_view(request): await asyncio.sleep(0.1) return JsonResponse({'message': 'Hello async'})
Attempts:
2 left
💡 Hint
Async views can use await inside and return normal HttpResponse or JsonResponse objects.
✗ Incorrect
Async views in Django can use await for async operations like asyncio.sleep. Returning JsonResponse sends JSON data as HTTP response body.
📝 Syntax
intermediate1:30remaining
Which option correctly defines an async Django view?
Select the option that correctly defines an async Django view function.
Attempts:
2 left
💡 Hint
Async views must be defined with async def and can return HttpResponse directly.
✗ Incorrect
Option C correctly uses async def and returns HttpResponse. Option C has invalid syntax. Option C tries to await a non-awaitable. Option C uses await outside async function.
🔧 Debug
advanced2:00remaining
What error occurs when awaiting a synchronous Django ORM call in async view?
Given this async view snippet, what error will it raise?
async def view(request):
user = await User.objects.get(id=1)
return HttpResponse(user.username)
Attempts:
2 left
💡 Hint
Django ORM calls are synchronous and cannot be awaited directly.
✗ Incorrect
Django ORM methods like get() are synchronous and return model instances, which are not awaitable. Awaiting them causes TypeError.
❓ state_output
advanced1:30remaining
What is the order of printed output in this async Django view?
Analyze the following async view and determine the order of printed lines in the server console.
Django
import asyncio from django.http import HttpResponse async def view(request): print('Start') await asyncio.sleep(0.05) print('Middle') await asyncio.sleep(0.05) print('End') return HttpResponse('Done')
Attempts:
2 left
💡 Hint
Await pauses execution until the awaited task completes.
✗ Incorrect
The prints happen sequentially with awaits in between, so output order is Start, Middle, End.
🧠 Conceptual
expert2:30remaining
Which statement about Django async views is TRUE?
Select the true statement about async views in Django.
Attempts:
2 left
💡 Hint
Think about how async views handle async and sync code.
✗ Incorrect
Async views can await async code but synchronous blocking calls like ORM must be handled carefully (e.g., using sync_to_async). They do not run in separate threads by default and can return any HttpResponse subclass.