📖 Scenario: You are building a simple Django web app that shows a greeting message based on the time of day. The app needs to decide what message to show when a user visits the page.
🎯 Goal: Build a Django view function that handles the request logic to decide the greeting message and returns an HttpResponse with it for display.
📋 What You'll Learn
Create a Django view function named greeting_view
Add a variable hour that stores the current hour (0-23)
Use if statements to decide the greeting message based on hour
Return an HttpResponse with the greeting message
💡 Why This Matters
🌍 Real World
Web apps need to decide what content to show users based on their request details, like time or user info.
💼 Career
Understanding how views handle request logic is essential for backend web development with Django.
Progress0 / 4 steps
1
Setup the Django view function
Create a Django view function called greeting_view that takes a request parameter.
Django
Hint
Start by defining a function named greeting_view that accepts request.
2
Add current hour variable
Inside greeting_view, create a variable called hour and set it to 14.
Django
Hint
Assign the number 14 to a variable named hour.
3
Add logic to decide greeting message
Add if statements inside greeting_view to set a variable message to 'Good morning' if hour is less than 12, otherwise 'Good afternoon'.
Django
Hint
Use if hour < 12 to set message to 'Good morning', else set it to 'Good afternoon'.
4
Return HttpResponse with greeting message
Return an HttpResponse from greeting_view that contains the message variable.
Django
Hint
Use return HttpResponse(message) to send the greeting back to the browser.
Practice
(1/5)
1. Why do Django views handle request logic instead of templates?
easy
A. Because templates are only for styling the page
B. Because views process data and decide what response to send
C. Because views store the database records
D. Because templates handle user authentication
Solution
Step 1: Understand the role of views
Views receive the user's request and decide what to do with it, like fetching data or processing input.
Step 2: Understand the role of templates
Templates only display the data and do not contain logic to process requests.
Final Answer:
Because views process data and decide what response to send -> Option B
2. Which of the following is the correct way to define a Django view function that handles a request?
easy
A. def my_view(request, response): return 'Hello'
B. def my_view(): return HttpResponse('Hello')
C. def my_view(request): print('Hello')
D. def my_view(request): return HttpResponse('Hello')
Solution
Step 1: Check function parameters
A Django view must accept at least one parameter named 'request' representing the user's request.
Step 2: Check return type
The view should return an HttpResponse object or similar, not just print or return a string.
Final Answer:
def my_view(request): return HttpResponse('Hello') -> Option D
Quick Check:
View(request) returns HttpResponse [OK]
Hint: View functions always take 'request' and return HttpResponse [OK]
Common Mistakes:
Omitting the request parameter
Returning plain strings instead of HttpResponse
Adding extra parameters not handled by Django
3. Given this view code:
from django.http import HttpResponse
def greet(request):
name = request.GET.get('name', 'Guest')
return HttpResponse(f'Hello, {name}!')
What will be the response if the user visits /greet?name=Anna?
medium
A. Hello, Anna!
B. Hello, Guest!
C. Hello, None!
D. Error: name parameter missing
Solution
Step 1: Understand request.GET.get usage
The code tries to get the 'name' parameter from the URL query string; if missing, it uses 'Guest'.
Step 2: Check the URL query parameter
The URL has '?name=Anna', so 'name' is 'Anna', not missing.
Final Answer:
Hello, Anna! -> Option A
Quick Check:
Query param 'name' = Anna [OK]
Hint: Check URL query parameters and default values [OK]
Common Mistakes:
Assuming default is always used
Confusing GET with POST data
Expecting an error if parameter is missing
4. This view code has an error:
def show(request):
user = request.POST['username']
return HttpResponse(f'User: {user}')
What is the likely error when a GET request is made to this view?
medium
A. KeyError because 'username' is missing in POST data
B. SyntaxError due to missing colon
C. TypeError because request.POST is not a dictionary
D. No error, returns empty user
Solution
Step 1: Understand request.POST usage
request.POST contains data only for POST requests; a GET request has no POST data.
Step 2: Accessing missing key causes error
Accessing request.POST['username'] when missing raises KeyError.
Final Answer:
KeyError because 'username' is missing in POST data -> Option A
Quick Check:
Missing POST key causes KeyError [OK]
Hint: Access POST keys safely or check request method first [OK]
Common Mistakes:
Assuming POST data exists on GET requests
Expecting empty string instead of error
Confusing KeyError with SyntaxError
5. You want a view to handle both GET and POST requests differently. Which code correctly handles this logic?
hard
A. def my_view(request):
if request.GET:
return HttpResponse('Posted')
else:
return HttpResponse('Got')
B. def my_view(request):
if request.POST:
return HttpResponse('Posted')
else:
return HttpResponse('Got')
C. def my_view(request):
if request.method == 'POST':
return HttpResponse('Posted')
else:
return HttpResponse('Got')
D. def my_view(request):
if request.method == 'GET':
return HttpResponse('Posted')
else:
return HttpResponse('Got')
Solution
Step 1: Check how to detect request method
Use request.method to check if the request is 'POST' or 'GET'.
Step 2: Verify correct branching
def my_view(request):
if request.method == 'POST':
return HttpResponse('Posted')
else:
return HttpResponse('Got') correctly returns 'Posted' for POST and 'Got' for others (like GET).
Final Answer:
def my_view(request):
if request.method == 'POST':
return HttpResponse('Posted')
else:
return HttpResponse('Got') -> Option C
Quick Check:
Use request.method to branch logic [OK]
Hint: Check request.method string, not request.POST or GET dict [OK]
Common Mistakes:
Checking if request.POST or request.GET is truthy instead of method
Mixing up GET and POST conditions
Returning wrong response for request types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.