0
0
Djangoframework~20 mins

Function-based views basics in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function-based Views Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!')
AA blank page with no content
BA JSON response with the message 'Hello, friend!'
CThe text 'Hello, friend!' displayed in the browser
DA server error because the view is missing a template
Attempts:
2 left
💡 Hint
HttpResponse sends plain text back to the browser.
📝 Syntax
intermediate
2: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!')
AIndent the return statement one level less
BAdd a colon after the function definition: def welcome(request):
CRemove the parentheses from the function definition
DChange HttpResponse to JsonResponse
Attempts:
2 left
💡 Hint
Function definitions in Python always end with a colon.
state_output
advanced
2: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')
A'Posted!'
BA 404 Not Found error
CA server error due to missing POST handling
D'Not a POST request'
Attempts:
2 left
💡 Hint
The view checks the HTTP method and returns different responses.
🔧 Debug
advanced
2: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)
AHttpResponse does not accept positional arguments after keyword arguments
BHttpResponse requires a template to render
CThe status code 200 is invalid
DThe view is missing a return statement
Attempts:
2 left
💡 Hint
Check the order of arguments passed to HttpResponse.
🧠 Conceptual
expert
2:00remaining
Which statement about Django function-based views is true?
Select the correct statement about function-based views in Django.
AThey can return any HttpResponse subclass, including redirects and errors
BThey automatically handle URL routing without configuration
CThey cannot access request data like POST or GET parameters
DThey must always return a rendered template using render()
Attempts:
2 left
💡 Hint
Think about what kinds of responses a view can send back.