0
0
Djangoframework~15 mins

Request parsing and response rendering in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Request parsing and response rendering in Django
📖 Scenario: You are building a simple Django view that receives a user's name via a URL query parameter and returns a greeting message as an HTTP response.
🎯 Goal: Create a Django view function that parses the name parameter from the request's GET data and returns a plain text greeting message including that name.
📋 What You'll Learn
Create a Django view function named greet_user
Parse the name parameter from request.GET
Set a default name if name is not provided
Return an HttpResponse with a greeting message including the name
Use plain text content type in the response
💡 Why This Matters
🌍 Real World
Parsing request data and sending responses is a core part of building web applications with Django.
💼 Career
Understanding how to handle HTTP requests and responses is essential for backend web developers working with Django.
Progress0 / 4 steps
1
Create 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 only argument.

2
Parse the 'name' parameter from the request
Inside the greet_user function, create a variable called name that gets the name parameter from request.GET. Use "Guest" as the default value if name is not provided.
Django
Need a hint?

Use request.GET.get('name', 'Guest') to safely get the name parameter or default to "Guest".

3
Create the greeting message
Still inside the greet_user function, create a variable called message that uses an f-string to say "Hello, {name}! Welcome to our site.".
Django
Need a hint?

Use an f-string to include the name variable inside the greeting message.

4
Return the HttpResponse with the greeting
Return an HttpResponse from the greet_user function with the message as content and set the content_type to "text/plain".
Django
Need a hint?

Use return HttpResponse(message, content_type='text/plain') to send the plain text response.