Bird
Raised Fist0
Djangoframework~20 mins

Process request and process response in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Django Request-Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view when receiving a GET request?
Consider this Django view function. What will be the HTTP response content when a GET request is made to this view?
Django
from django.http import HttpResponse

def my_view(request):
    if request.method == 'GET':
        return HttpResponse('Hello, GET request!')
    else:
        return HttpResponse('Not a GET request')
AHello, GET request!
BNot a GET request
CRaises an error because request.method is undefined
DEmpty response with status 200
Attempts:
2 left
💡 Hint
Check the request.method attribute for GET requests.
state_output
intermediate
2:00remaining
What is the value of response.status_code after this view runs?
Given this Django view, what will be the HTTP status code of the response object returned?
Django
from django.http import HttpResponse

def status_view(request):
    response = HttpResponse('OK')
    response.status_code = 201
    return response
A200
B201
C404
D500
Attempts:
2 left
💡 Hint
Look at how the status_code attribute is set on the response.
📝 Syntax
advanced
2:00remaining
Which option correctly accesses POST data in a Django view?
You want to get the value of the 'username' field sent via POST in a Django view. Which code snippet correctly retrieves it?
Django
def post_view(request):
    # retrieve username from POST data
    pass
Ausername = request.data['username']
Busername = request.GET['username']
Cusername = request.body['username']
Dusername = request.POST['username']
Attempts:
2 left
💡 Hint
POST data is accessed via request.POST dictionary.
🔧 Debug
advanced
2:00remaining
What error does this Django view raise when called?
Analyze this Django view code. What error will it raise when a request is made?
Django
from django.http import HttpResponse

def error_view(request):
    return HttpResponse('Hello') + ' World'
ATypeError: unsupported operand type(s) for +: 'HttpResponse' and 'str'
BSyntaxError: invalid syntax
CNameError: name 'HttpResponse' is not defined
DNo error, returns 'Hello World'
Attempts:
2 left
💡 Hint
Check the types involved in the + operation.
🧠 Conceptual
expert
3:00remaining
Which option best describes how Django processes a request and returns a response?
Select the option that correctly describes the sequence Django follows when processing an HTTP request and returning a response.
ADjango receives request → URL dispatcher selects view → Middleware processes request → View returns HttpResponse → Response sent to client
BDjango receives request → View processes request → Middleware processes response → URL dispatcher selects view → Response sent to client
CDjango receives request → Middleware processes request → URL dispatcher selects view → View returns HttpResponse → Middleware processes response → Response sent to client
DDjango receives request → Middleware processes response → URL dispatcher selects view → View returns HttpResponse → Response sent to client
Attempts:
2 left
💡 Hint
Think about the order of middleware and URL routing in Django's request cycle.

Practice

(1/5)
1. What does the request object in a Django view primarily contain?
easy
A. Information sent by the user, like form data and headers
B. The HTML template to render
C. The final content sent back to the user
D. Database connection details

Solution

  1. Step 1: Understand the role of the request object

    The request object holds all data sent by the user, such as form inputs, cookies, and headers.
  2. Step 2: Differentiate request from response

    The response object is what the server sends back, not the request.
  3. Final Answer:

    Information sent by the user, like form data and headers -> Option A
  4. Quick Check:

    Request = User data [OK]
Hint: Request holds user input; response sends output [OK]
Common Mistakes:
  • Confusing request with response
  • Thinking request contains templates
  • Assuming request holds server data
2. Which of the following is the correct way to return a simple text response in a Django view?
easy
A. return HttpResponse('Hello World')
B. return render('Hello World')
C. return JsonResponse('Hello World')
D. return redirect('Hello World')

Solution

  1. Step 1: Identify the correct response class for text

    HttpResponse is used to send plain text or HTML content back to the user.
  2. Step 2: Check other options

    render is for templates, JsonResponse for JSON data, redirect for URL redirects.
  3. Final Answer:

    return HttpResponse('Hello World') -> Option A
  4. Quick Check:

    Text response uses HttpResponse [OK]
Hint: Use HttpResponse for plain text output [OK]
Common Mistakes:
  • Using render without a template
  • Using JsonResponse for plain text
  • Confusing redirect with response content
3. Given this Django view code, what will be the output when accessed?
from django.http import JsonResponse

def my_view(request):
    data = {'name': 'Alice', 'age': 30}
    return JsonResponse(data)
medium
A. name=Alice&age=30
B. {'name': 'Alice', 'age': 30}
C. {"name": "Alice", "age": 30}
D. An error because JsonResponse needs a string

Solution

  1. Step 1: Understand JsonResponse output format

    JsonResponse converts the Python dictionary into a JSON string with double quotes.
  2. Step 2: Check the output format

    The output is a JSON string: {"name": "Alice", "age": 30} with double quotes, not Python dict syntax.
  3. Final Answer:

    {"name": "Alice", "age": 30} -> Option C
  4. Quick Check:

    JsonResponse outputs JSON string [OK]
Hint: JsonResponse outputs JSON with double quotes [OK]
Common Mistakes:
  • Expecting Python dict syntax in output
  • Confusing URL query string with JSON
  • Thinking JsonResponse needs string input
4. What is wrong with this Django view code?
from django.http import HttpResponse

def bad_view(request):
    response = HttpResponse('Hello')
    response.status_code = '404'
    return response
medium
A. HttpResponse cannot have status_code set manually
B. status_code must be an integer, not a string
C. Missing return statement
D. HttpResponse requires a JSON object

Solution

  1. Step 1: Check the status_code assignment

    Status codes must be integers like 404, not strings like '404'.
  2. Step 2: Confirm HttpResponse usage

    HttpResponse allows setting status_code manually but it must be int.
  3. Final Answer:

    status_code must be an integer, not a string -> Option B
  4. Quick Check:

    status_code = int, not string [OK]
Hint: status_code must be int, not string [OK]
Common Mistakes:
  • Using string for status_code
  • Thinking HttpResponse forbids status_code
  • Forgetting to return response
5. You want to create a Django view that processes a POST request with JSON data and returns a JSON response with a message and the original data. Which code snippet correctly handles this?
hard
A. def view(request): data = request.GET return JsonResponse({'message': 'Received', 'data': data})
B. def view(request): data = request.POST return HttpResponse({'message': 'Received', 'data': data})
C. def view(request): data = request.body return JsonResponse(data)
D. def view(request): data = json.loads(request.body) return JsonResponse({'message': 'Received', 'data': data})

Solution

  1. Step 1: Parse JSON from POST request body

    request.body contains raw bytes; json.loads converts it to a Python dict.
  2. Step 2: Return JsonResponse with message and data

    JsonResponse correctly serializes the Python dict to JSON for response.
  3. Step 3: Check other options

    def view(request): data = request.POST return HttpResponse({'message': 'Received', 'data': data}) wrongly uses HttpResponse with dict, def view(request): data = request.GET return JsonResponse({'message': 'Received', 'data': data}) uses GET instead of POST, def view(request): data = request.body return JsonResponse(data) returns raw bytes without parsing.
  4. Final Answer:

    def view(request): data = json.loads(request.body) return JsonResponse({'message': 'Received', 'data': data}) -> Option D
  5. Quick Check:

    Parse JSON body, respond with JsonResponse [OK]
Hint: Parse JSON from request.body, respond with JsonResponse [OK]
Common Mistakes:
  • Using request.POST for JSON body
  • Returning dict in HttpResponse
  • Not parsing JSON before response