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
Recall & Review
beginner
What is a request in Django?
A request is an object that Django receives from a user’s browser. It contains all the information about what the user wants, like the URL, form data, and headers.
Click to reveal answer
beginner
What does Django use to send data back to the user?
Django sends a response object back to the user’s browser. This response contains the content to display, like HTML, JSON, or a redirect.
Click to reveal answer
intermediate
How does Django handle a request internally?
Django takes the request, matches its URL to a view function, runs that function, and then returns the response generated by the view.
Click to reveal answer
intermediate
What is the role of middleware in processing requests and responses?
Middleware is code that runs before and after the view. It can modify the request before it reaches the view or change the response before it goes back to the user.
Click to reveal answer
beginner
What is the difference between request.GET and request.POST?
request.GET holds data sent in the URL query string, usually from links or forms with method GET. request.POST holds data sent in the body of a POST request, usually from forms that submit data.
Click to reveal answer
What does Django use to decide which code runs for a request?
ADatabase schema
BURL routing
CTemplate engine
DMiddleware stack
✗ Incorrect
Django uses URL routing to match the request URL to the correct view function.
Which object contains the data sent by the user in a form submission?
Arequest.GET
Bresponse.content
Crequest.POST
Drequest.META
✗ Incorrect
request.POST contains form data sent via POST method.
What can middleware do in Django?
AModify both request and response
BOnly modify the response
COnly modify the request
DOnly handle database queries
✗ Incorrect
Middleware can modify both the request before the view and the response after the view.
What type of object does a Django view return?
ARequest object
BMiddleware object
CTemplate object
DResponse object
✗ Incorrect
A Django view returns a response object that is sent back to the user.
Where does Django store HTTP headers from the request?
Arequest.META
Brequest.headers
Crequest.POST
Dresponse.headers
✗ Incorrect
HTTP headers are stored in request.META as a dictionary.
Explain the journey of a user request through Django until a response is sent back.
Think about what happens from when you type a URL to when you see a page.
You got /5 concepts.
Describe how you can access form data sent by a user in Django.
Consider the HTTP methods and where data is stored in the request object.
You got /4 concepts.
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
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.
Step 2: Differentiate request from response
The response object is what the server sends back, not the request.
Final Answer:
Information sent by the user, like form data and headers -> Option A
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
Step 1: Identify the correct response class for text
HttpResponse is used to send plain text or HTML content back to the user.
Step 2: Check other options
render is for templates, JsonResponse for JSON data, redirect for URL redirects.
Final Answer:
return HttpResponse('Hello World') -> Option A
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
Step 1: Understand JsonResponse output format
JsonResponse converts the Python dictionary into a JSON string with double quotes.
Step 2: Check the output format
The output is a JSON string: {"name": "Alice", "age": 30} with double quotes, not Python dict syntax.
Final Answer:
{"name": "Alice", "age": 30} -> Option C
Quick Check:
JsonResponse outputs JSON string [OK]
Hint: JsonResponse outputs JSON with double quotes [OK]
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
Step 1: Check the status_code assignment
Status codes must be integers like 404, not strings like '404'.
Step 2: Confirm HttpResponse usage
HttpResponse allows setting status_code manually but it must be int.
Final Answer:
status_code must be an integer, not a string -> Option B
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
Step 1: Parse JSON from POST request body
request.body contains raw bytes; json.loads converts it to a Python dict.
Step 2: Return JsonResponse with message and data
JsonResponse correctly serializes the Python dict to JSON for response.
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.
Final Answer:
def view(request):
data = json.loads(request.body)
return JsonResponse({'message': 'Received', 'data': data}) -> Option D
Quick Check:
Parse JSON body, respond with JsonResponse [OK]
Hint: Parse JSON from request.body, respond with JsonResponse [OK]