0
0
Djangoframework~30 mins

Why views handle request logic in Django - See It in Action

Choose your learning style9 modes available
Why Views Handle Request Logic in Django
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use return HttpResponse(message) to send the greeting back to the browser.