0
0
Djangoframework~10 mins

Async views basics in Django - Interactive Code Practice

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

Complete the code to define an async view function in Django.

Django
from django.http import JsonResponse

async def my_view(request):
    data = {'message': 'Hello, async!'}
    return [1](data)
Drag options to blanks, or click blank then click option'
AJsonResponse
BHttpResponse
Crender
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using HttpResponse without JSON formatting
Trying to use render which expects a template
Returning redirect which is for URL redirection
2fill in blank
medium

Complete the code to await an async function inside the async view.

Django
import asyncio
from django.http import JsonResponse

async def fetch_data():
    await asyncio.sleep(1)
    return {'status': 'done'}

async def my_view(request):
    result = [1] fetch_data()
    return JsonResponse(result)
Drag options to blanks, or click blank then click option'
Afetch_data
Bawait
Casync
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the async function without await causing a coroutine object to be returned
Using async keyword incorrectly inside the function body
Using yield which is for generators
3fill in blank
hard

Fix the error in the async view by completing the code to import the correct decorator.

Django
from django.http import JsonResponse
from django.views.decorators.csrf import [1]

@[1]  # Decorator to exempt CSRF for async view
async def my_view(request):
    return JsonResponse({'message': 'No CSRF check'})
Drag options to blanks, or click blank then click option'
Acache_page
Blogin_required
Crequire_http_methods
Dcsrf_exempt
Attempts:
3 left
💡 Hint
Common Mistakes
Using login_required which is for authentication
Using require_http_methods which restricts HTTP methods
Using cache_page which caches the response
4fill in blank
hard

Fill both blanks to create an async view that reads a query parameter and returns it in JSON.

Django
from django.http import JsonResponse

async def echo_view(request):
    param = request.GET.get([1], 'default')
    return JsonResponse([2]: param)
Drag options to blanks, or click blank then click option'
A'name'
B'value'
C'param'
D'key'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys causing missing data
Not using quotes around string keys
Mixing up parameter and JSON keys
5fill in blank
hard

Fill all three blanks to create an async view that waits for two async tasks and returns combined results.

Django
import asyncio
from django.http import JsonResponse

async def task1():
    await asyncio.sleep(1)
    return 'first'

async def task2():
    await asyncio.sleep(1)
    return 'second'

async def combined_view(request):
    result1, result2 = await asyncio.gather([1](), [2]())
    return JsonResponse([3]: f'{result1} and {result2}')
Drag options to blanks, or click blank then click option'
Atask1
Btask2
C'result'
D'output'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling functions without parentheses
Using wrong keys in JSON response
Not awaiting both tasks together