0
0
Djangoframework~10 mins

Why async matters in Django - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an asynchronous view in Django.

Django
async def my_view(request):
    return [1]('Hello, async!')
Drag options to blanks, or click blank then click option'
AAsyncResponse
BHttpResponseAsync
CResponse
DHttpResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent async response class.
2fill in blank
medium

Complete the code to await an asynchronous database call in a Django async view.

Django
async def get_user(request):
    user = await User.objects.[1](id=1)
    return HttpResponse(user.username)
Drag options to blanks, or click blank then click option'
Afetch
Baget
Cget
Dget_async
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous ORM methods inside async views.
3fill in blank
hard

Fix the error in the async view by choosing the correct way to call a synchronous function.

Django
async def sync_call_view(request):
    result = [1](sync_function)()
    return HttpResponse(result)
Drag options to blanks, or click blank then click option'
Aawait
Basync_to_sync
Csync_to_async
Drun_sync
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async_to_sync' which is for the opposite direction.
4fill in blank
hard

Fill both blanks to create an async view that fetches data and returns JSON response.

Django
from django.http import JsonResponse

async def data_view(request):
    data = await Model.objects.[1]().values().afetchall()
    return [2](data, safe=False)
Drag options to blanks, or click blank then click option'
Aall
BJsonResponse
Cfilter
DHttpResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous ORM methods or wrong response classes.
5fill in blank
hard

Fill all three blanks to write an async Django view that calls a sync function safely and returns its result.

Django
from asgiref.sync import [1]

async def combined_view(request):
    result = await [2](sync_func)()
    return [3](str(result))
Drag options to blanks, or click blank then click option'
Async_to_async
CHttpResponse
DJsonResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing sync_to_async with async_to_sync.
Using JsonResponse when returning plain text.