Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a function-based view in Django?
easy
A. To handle web requests and return responses
B. To define database models
C. To style HTML pages with CSS
D. To manage user sessions automatically

Solution

  1. Step 1: Understand the role of views in Django

    Views are responsible for processing web requests and returning responses.
  2. Step 2: Identify the function-based view role

    Function-based views are simple Python functions that take a request and return a response.
  3. Final Answer:

    To handle web requests and return responses -> Option A
  4. Quick Check:

    Function-based views handle requests = A [OK]
Hint: Views handle requests and responses in Django [OK]
Common Mistakes:
  • Confusing views with models
  • Thinking views handle styling
  • Assuming views manage sessions automatically
2. Which of the following is the correct way to define a simple function-based view in Django?
easy
A. def my_view(request): print('Hello')
B. class my_view: return HttpResponse('Hello')
C. def my_view(): return HttpResponse('Hello')
D. def my_view(request): return HttpResponse('Hello')

Solution

  1. Step 1: Check function signature for views

    A function-based view must accept a request parameter.
  2. Step 2: Verify the return value

    The view must return an HttpResponse object, not just print or omit request.
  3. Final Answer:

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

    Function with request param and HttpResponse return = A [OK]
Hint: View functions need request param and return HttpResponse [OK]
Common Mistakes:
  • Omitting the request parameter
  • Using class syntax instead of function
  • Printing instead of returning HttpResponse
3. Given this function-based view code, what will be the HTTP response content?
from django.http import HttpResponse

def greet(request):
    name = request.GET.get('name', 'Guest')
    return HttpResponse(f'Hello, {name}!')
medium
A. Hello, None!
B. Hello, Guest!
C. Hello, name!
D. Error: missing name parameter

Solution

  1. Step 1: Understand request.GET.get usage

    The code tries to get 'name' from query parameters; if missing, defaults to 'Guest'.
  2. Step 2: Determine response content when no name given

    If no 'name' parameter is in the URL, it uses 'Guest', so response is 'Hello, Guest!'.
  3. Final Answer:

    Hello, Guest! -> Option B
  4. Quick Check:

    Missing name defaults to Guest = B [OK]
Hint: GET.get with default returns default if param missing [OK]
Common Mistakes:
  • Assuming missing param causes error
  • Confusing string literal with variable name
  • Expecting None instead of default value
4. Identify the error in this function-based view code:
from django.http import HttpResponse

def my_view():
    return HttpResponse('Hi')
medium
A. Return statement syntax is incorrect
B. HttpResponse is not imported
C. Missing request parameter in function definition
D. Function name is invalid

Solution

  1. Step 1: Check function parameters for views

    Function-based views must accept a request parameter to receive HTTP requests.
  2. Step 2: Verify imports and syntax

    HttpResponse is imported correctly and return syntax is valid; function name is valid.
  3. Final Answer:

    Missing request parameter in function definition -> Option C
  4. Quick Check:

    View functions need request param = C [OK]
Hint: Always include request parameter in view functions [OK]
Common Mistakes:
  • Omitting request parameter
  • Forgetting to import HttpResponse
  • Misnaming the function
5. You want to create a function-based view that returns a 404 error if a query parameter 'id' is missing. Which code snippet correctly implements this?
hard
A. def view(request): if request.GET.get('id') is None: return HttpResponse('Not found', status=404) return HttpResponse('Found')
B. def view(request): if request.GET['id'] is None: return HttpResponse('Not found', status=404) return HttpResponse('Found')
C. def view(request): if request.GET.get('id') == '': return HttpResponse('Not found', status=404) return HttpResponse('Found')
D. def view(request): if 'id' not in request.POST: return HttpResponse('Not found', status=404) return HttpResponse('Found')

Solution

  1. Step 1: Check how to safely get query parameters

    Using request.GET.get('id') returns None if 'id' is missing, avoiding KeyError.
  2. Step 2: Verify condition to return 404

    If get returns None, return HttpResponse with status=404; else return 'Found'.
  3. Final Answer:

    def view(request): if request.GET.get('id') is None: return HttpResponse('Not found', status=404) return HttpResponse('Found') -> Option A
  4. Quick Check:

    Use GET.get to check missing param safely = D [OK]
Hint: Use request.GET.get to avoid KeyError when checking params [OK]
Common Mistakes:
  • Using request.GET['id'] without checking existence
  • Checking for empty string instead of None
  • Confusing request.GET with request.POST
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard