Complete the code to define an async view function in Django.
from django.http import JsonResponse async def my_view(request): data = {'message': 'Hello, async!'} return [1](data)
The async view should return a JsonResponse to send JSON data back to the client.
Complete the code to await an async function inside the async view.
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)
Inside an async function, you use await to wait for another async function to finish.
Fix the error in the async view by completing the code to import the correct decorator.
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'})
The csrf_exempt decorator disables CSRF checks, which is often needed for async views handling POST requests.
Fill both blanks to create an async view that reads a query parameter and returns it in JSON.
from django.http import JsonResponse async def echo_view(request): param = request.GET.get([1], 'default') return JsonResponse([2]: param)
The query parameter key is 'name', and the JSON response key is 'param' to return the value.
Fill all three blanks to create an async view that waits for two async tasks and returns combined results.
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}')
Use task1 and task2 functions in asyncio.gather, and return JSON with key 'result'.