Challenge - 5 Problems
Function-based Views Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django function-based view return?
Consider this Django view function. What will the user see when visiting the URL linked to this view?
Django
from django.http import HttpResponse def greet(request): return HttpResponse('Hello, friend!')
Attempts:
2 left
💡 Hint
HttpResponse sends plain text back to the browser.
✗ Incorrect
The view returns an HttpResponse with the string 'Hello, friend!'. This text appears in the browser as plain content.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Django view
Which option correctly fixes the syntax error in this function-based view?
Django
from django.http import HttpResponse def welcome(request) return HttpResponse('Welcome!')
Attempts:
2 left
💡 Hint
Function definitions in Python always end with a colon.
✗ Incorrect
The function definition is missing a colon at the end of the line. Adding ':' fixes the syntax error.
❓ state_output
advanced2:00remaining
What is the output of this view when accessed with a GET request?
Analyze this Django view. What will the user see when they visit the URL with a GET request?
Django
from django.http import HttpResponse def check_method(request): if request.method == 'POST': return HttpResponse('Posted!') else: return HttpResponse('Not a POST request')
Attempts:
2 left
💡 Hint
The view checks the HTTP method and returns different responses.
✗ Incorrect
Since the request is GET, the else branch runs, returning 'Not a POST request'.
🔧 Debug
advanced2:00remaining
Why does this Django view cause a runtime error?
This view raises an error when accessed. What is the cause?
Django
from django.http import HttpResponse def broken_view(request): return HttpResponse('All good!', status=200)
Attempts:
2 left
💡 Hint
Check the order of arguments passed to HttpResponse.
✗ Incorrect
HttpResponse expects the content as the first argument. Passing status=200 before content causes a TypeError.
🧠 Conceptual
expert2:00remaining
Which statement about Django function-based views is true?
Select the correct statement about function-based views in Django.
Attempts:
2 left
💡 Hint
Think about what kinds of responses a view can send back.
✗ Incorrect
Function-based views can return any HttpResponse subclass, such as HttpResponseRedirect or HttpResponseNotFound.