Bird
Raised Fist0
Djangoframework~10 mins

Function-based views basics in Django - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function to create HTTP responses in Django.

Django
from django.http import [1]
Drag options to blanks, or click blank then click option'
AHttpRequest
BHttpResponse
Crender
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing HttpRequest instead of HttpResponse
Using render instead of HttpResponse for simple responses
2fill in blank
medium

Complete the function-based view to return a simple HTTP response.

Django
def home_view(request):
    return [1]("Hello, world!")
Drag options to blanks, or click blank then click option'
Arender
BJsonResponse
CHttpResponse
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Using render without a template
Returning a string directly instead of an HttpResponse
3fill in blank
hard

Fix the error in the view function to correctly accept the request parameter.

Django
def about_view([1]):
    return HttpResponse("About page")
Drag options to blanks, or click blank then click option'
Acontext
Bself
Cresponse
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' which is for class methods
Using 'response' or 'context' which are not request objects
4fill in blank
hard

Fill both blanks to import and use the shortcut to render a template in a function-based view.

Django
from django.shortcuts import [1]

def contact_view(request):
    return [2](request, 'contact.html')
Drag options to blanks, or click blank then click option'
Arender
BHttpResponse
Credirect
Dget_object_or_404
Attempts:
3 left
💡 Hint
Common Mistakes
Importing render but returning HttpResponse directly
Using redirect instead of render for templates
5fill in blank
hard

Fill all three blanks to create a function-based view that renders a template with context data.

Django
from django.shortcuts import [1]

def profile_view(request):
    context = {'name': [2]
    return [3](request, 'profile.html', context)
Drag options to blanks, or click blank then click option'
Arender
B'Alice'
DHttpResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing context to render
Using HttpResponse instead of render for templates with context

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