A function-based view is a Python function that takes a web request and returns a web response. It handles the logic for a specific URL in a simple, direct way.
Click to reveal answer
beginner
What parameter does a function-based view always receive?
A function-based view always receives a request object as its first parameter, which contains details about the web request.
Click to reveal answer
beginner
How do you return a simple HTML response from a function-based view?
Use HttpResponse from django.http to return HTML content. For example: return HttpResponse('<h1>Hello</h1>')
Click to reveal answer
beginner
How do you connect a function-based view to a URL in Django?
In the <code>urls.py</code> file, import the view function and add a path with the URL pattern and the view function name. For example: <br><code>path('home/', views.home_view)</code>
Click to reveal answer
beginner
Why might you choose a function-based view over a class-based view?
Function-based views are simple and easy to understand for small or straightforward tasks. They are good for beginners and quick setups without extra structure.
Click to reveal answer
What does a function-based view in Django always take as its first argument?
Atemplate
Bresponse
Crequest
Durl
✗ Incorrect
A function-based view always takes the request object as its first argument.
Which Django module provides the HttpResponse class used in function-based views?
Adjango.http
Bdjango.views
Cdjango.urls
Ddjango.shortcuts
✗ Incorrect
HttpResponse is imported from django.http to send back responses.
How do you link a function-based view to a URL pattern?
ABy adding the view function to the settings.py
BBy importing the view in urls.py and adding it to urlpatterns
CBy decorating the view function with @url
DBy naming the view function the same as the URL
✗ Incorrect
You import the view in urls.py and add it to urlpatterns with a path.
What is the typical return type of a function-based view?
AAn HttpResponse object
BA string
CA dictionary
DA list
✗ Incorrect
Function-based views return an HttpResponse object that contains the response data.
Which of these is a reason to use function-based views?
AThey require less code for large projects
BThey are more complex and powerful than class-based views
CThey automatically handle forms
DThey are easier to understand for simple tasks
✗ Incorrect
Function-based views are simpler and easier to understand for beginners and small tasks.
Explain how a function-based view works in Django from receiving a request to sending a response.
Think about the steps from when a user visits a URL to what the server sends back.
You got /4 concepts.
Describe how you would set up a new function-based view and connect it to a URL in a Django project.
Consider both the view code and the URL configuration.
You got /4 concepts.
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
Step 1: Understand the role of views in Django
Views are responsible for processing web requests and returning responses.
Step 2: Identify the function-based view role
Function-based views are simple Python functions that take a request and return a response.
Final Answer:
To handle web requests and return responses -> Option A
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
Step 1: Check function signature for views
A function-based view must accept a request parameter.
Step 2: Verify the return value
The view must return an HttpResponse object, not just print or omit request.
Final Answer:
def my_view(request): return HttpResponse('Hello') -> Option D
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
Step 1: Understand request.GET.get usage
The code tries to get 'name' from query parameters; if missing, defaults to 'Guest'.
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!'.
Final Answer:
Hello, Guest! -> Option B
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
Step 1: Check function parameters for views
Function-based views must accept a request parameter to receive HTTP requests.
Step 2: Verify imports and syntax
HttpResponse is imported correctly and return syntax is valid; function name is valid.
Final Answer:
Missing request parameter in function definition -> Option C
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
Step 1: Check how to safely get query parameters
Using request.GET.get('id') returns None if 'id' is missing, avoiding KeyError.
Step 2: Verify condition to return 404
If get returns None, return HttpResponse with status=404; else return 'Found'.
Final Answer:
def view(request):
if request.GET.get('id') is None:
return HttpResponse('Not found', status=404)
return HttpResponse('Found') -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.