0
0
Djangoframework~20 mins

Async views basics in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Django 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 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'})
ATypeError: object JsonResponse can't be awaited
B{"message": "Hello async"}
CHttpResponse with empty body
DRuntimeError: This event loop is already running
Attempts:
2 left
💡 Hint
Async views can use await inside and return normal HttpResponse or JsonResponse objects.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines an async Django view?
Select the option that correctly defines an async Django view function.
Adef view(request): await HttpResponse('Hi')
Bdef async view(request): return HttpResponse('Hi')
Casync def view(request): return HttpResponse('Hi')
Dasync def view(request): await HttpResponse('Hi')
Attempts:
2 left
💡 Hint
Async views must be defined with async def and can return HttpResponse directly.
🔧 Debug
advanced
2: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)
ATypeError: 'User' object is not awaitable
BRuntimeError: Cannot run sync code in async context
COperationalError: database connection lost
DNo error, returns username
Attempts:
2 left
💡 Hint
Django ORM calls are synchronous and cannot be awaited directly.
state_output
advanced
1: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')
AStart, Middle, End
BStart, End, Middle
CEnd, Middle, Start
DMiddle, Start, End
Attempts:
2 left
💡 Hint
Await pauses execution until the awaited task completes.
🧠 Conceptual
expert
2:30remaining
Which statement about Django async views is TRUE?
Select the true statement about async views in Django.
AAsync views can directly call synchronous ORM methods without issues.
BAsync views must always return JsonResponse objects.
CAsync views run in a separate thread by default.
DAsync views allow awaiting asynchronous code but must avoid blocking sync calls.
Attempts:
2 left
💡 Hint
Think about how async views handle async and sync code.