Bird
0
0

Which of the following is the correct way to define an async view in Django?

easy📝 Syntax Q12 of 15
Django - Async Django
Which of the following is the correct way to define an async view in Django?
Adef my_view(request): return HttpResponse('Hello')
Basync def my_view(request): return HttpResponse('Hello')
Casync def my_view(request): await HttpResponse('Hello')
Ddef async my_view(request): return HttpResponse('Hello')
Step-by-Step Solution
Solution:
  1. Step 1: Recall async view syntax

    Async views must be defined with async def and can return a response directly.
  2. Step 2: Check each option

    async def my_view(request): return HttpResponse('Hello') correctly uses async def and returns a response. def my_view(request): return HttpResponse('Hello') is a normal sync view. async def my_view(request): await HttpResponse('Hello') wrongly uses await on a response object, which is not awaitable. def async my_view(request): return HttpResponse('Hello') has invalid syntax.
  3. Final Answer:

    async def my_view(request): return HttpResponse('Hello') -> Option B
  4. Quick Check:

    Async view syntax = async def [OK]
Quick Trick: Async views start with 'async def' keyword [OK]
Common Mistakes:
MISTAKES
  • Using 'def' instead of 'async def'
  • Awaiting non-awaitable objects like HttpResponse
  • Incorrect function declaration syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes