0
0
Djangoframework~20 mins

Process request and process response in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Process request and process response in Django
📖 Scenario: You are building a simple Django web app that greets users by name. The app will receive a user's name from a URL and respond with a personalized greeting.
🎯 Goal: Create a Django view that processes the incoming HTTP request to get the user's name from the URL, then returns an HTTP response with a greeting message including that name.
📋 What You'll Learn
Create a Django view function named greet_user
Extract the name parameter from the URL request
Return an HttpResponse with the text 'Hello, <name>!'
Use Django's HttpResponse class for the response
💡 Why This Matters
🌍 Real World
Web applications often need to read data sent by users in requests and respond with personalized content.
💼 Career
Understanding how to process requests and send responses is fundamental for backend web developers working with Django.
Progress0 / 4 steps
1
Set up the Django view function
Create a Django view function called greet_user that takes a single parameter request.
Django
Need a hint?

Start by defining a function named greet_user that accepts request as its argument.

2
Extract the name parameter from the request
Inside the greet_user function, get the name parameter from the URL query string using request.GET.get('name') and store it in a variable called user_name.
Django
Need a hint?

Use request.GET.get('name') to safely get the name from the URL query parameters.

3
Create the greeting message
Still inside greet_user, create a variable called message that uses an f-string to say 'Hello, <user_name>!'.
Django
Need a hint?

Use an f-string to insert user_name into the greeting message.

4
Return the HTTP response
Return an HttpResponse object from greet_user with the message as its content.
Django
Need a hint?

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