Bird
0
0

What is wrong with this Django async view code?

medium📝 Debug Q7 of 15
Django - Async Django
What is wrong with this Django async view code?
async def view(request):
    data = await sync_fetch()
    return JsonResponse({'data': data})

Assuming sync_fetch is a synchronous function.
ARequest parameter should be optional
BJsonResponse cannot be returned from async views
CMissing async keyword on sync_fetch
DCannot await a synchronous function
Step-by-Step Solution
Solution:
  1. Step 1: Understand await usage

    Await can only be used with async functions or awaitable objects.
  2. Step 2: Identify misuse of await

    sync_fetch is synchronous, so awaiting it causes a runtime error.
  3. Final Answer:

    Cannot await a synchronous function -> Option D
  4. Quick Check:

    Await only async functions = A [OK]
Quick Trick: Never await sync functions directly [OK]
Common Mistakes:
MISTAKES
  • Awaiting sync functions
  • Thinking JsonResponse is async-only
  • Misunderstanding request parameter rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes