Question 4Medium
In what order does middleware process a request and its response?
- Both top to bottom, so the response passes through the same order as the request
- Requests top to bottom; responses bottom to top
- Both bottom to top
- Random
Answer: B. Requests top to bottom; responses bottom to top
Explanation
Like layers of an onion around the view.
Step-by-step solution
- Request goes in.
- Response goes out in reverse.
Why the other options are wrong
- A. Response unwinds in reverse.
- C. Request is top to bottom.
- D. Order is deterministic.
Question 5Hard
Where should SECRET_KEY and DB passwords come from in production?
- A settings.py file committed in the Git repo so every developer has the same values
- Environment variables or a secrets manager, not committed code
- A comment in urls.py
- The admin site
Answer: B. Environment variables or a secrets manager, not committed code
Explanation
Keep them out of version control.
Step-by-step solution
- Read from env.
- Never commit.
Why the other options are wrong
- A. Leaks on commit.
- C. A comment in urls.py is source code in the repository and is not read by Django as configuration.
- D. Not configurable there.
Question 6Hard
What does a middleware's __call__(request) do in function-based style get_response?
- Returns the rendered template directly, so the view and later layers are skipped
- Calls the view directly
- Calls get_response(request) to pass control onward, then can modify the response
- Reads the database
Answer: C. Calls get_response(request) to pass control onward, then can modify the response
Explanation
Code before the get_response(request) call runs on the request; code after it runs on the response.
Step-by-step solution
- Code in
__call__ before get_response(request) runs on the request. get_response(request) passes control to the next layer (the next middleware or the view) and returns its response.- Code after it can change the response, and
__call__ then returns it.
Why the other options are wrong
- A. A normal middleware does not render a template; it passes the request on with
get_response. - B.
get_response handles the next layer, which may be the view or another middleware. - D. Middleware wraps the request and response; reading the database is the view's or ORM's job.
Tip: Before get_response is request time; after it is response time.