Sample questions
Question 1Easy
Where do you list the middleware classes used by a Django project?
- In urls.py, next to the URL patterns for each view
- In manage.py, which runs management commands from the shell
- In settings.py, in the MIDDLEWARE list
- In wsgi.py, which exposes the application to the web server
Answer: C. In settings.py, in the MIDDLEWARE list
Explanation
Django reads the MIDDLEWARE setting in settings.py, a list of dotted paths to middleware classes. Each request passes through them in list order.
Step-by-step solution
- Project settings live in settings.py.
- MIDDLEWARE is a list of dotted class paths.
- Django builds the chain from that list.
Why the other options are wrong
- A. urls.py maps URLs to views; middleware is configured in settings.
- B. manage.py is the command-line entry point and has no middleware setting.
- D. wsgi.py only exposes the application for the web server.
Tip: The first middleware in the list is the outermost one.
Question 2Medium
When is process_template_response() called?
- Before the URL is matched, while Django still resolves the path
- Only for 404 pages, when no view matches the requested URL
- After a view returns a TemplateResponse, before it is rendered
- Once when the server starts, while it loads the settings file
Answer: C. After a view returns a TemplateResponse, before it is rendered
Explanation
If a view returns a TemplateResponse, or any response with render(), Django calls process_template_response after the view and before the response is rendered. A middleware can change the response there.
Step-by-step solution
- The view returns a TemplateResponse.
- The response has a render() method but is not rendered yet.
- Django calls process_template_response on the middleware.
- Rendering happens afterwards.
Why the other options are wrong
- A. URL matching happens before any view or response hook runs.
- B. This hook is not limited to 404 pages; it applies to any response that can be rendered.
- D. The hook runs for each request whose view returns a template response.
Tip: Use it to change the template context before the response is rendered.
Question 3Medium
In what order does Django process the MIDDLEWARE list for a request and a response?
- Bottom to top for both the request and the response
- Top to bottom for both the request and the response
- Down the list for requests, up for responses
- In random order decided separately for each request
Answer: C. Down the list for requests, up for responses
Explanation
Middleware wraps the view like layers of an onion.
Step-by-step solution
- Middleware wraps the view like layers of an onion.
- On a request, Django runs the MIDDLEWARE list from the top down.
- On the response, the same middleware runs back up the list.
- So the answer is option C: down for requests, up for responses.
Why the other options are wrong
- A. Requests run top to bottom, not bottom to top.
- B. Responses run back up the list, not down.
- D. The order follows the list and is not random.
Tip: The first middleware in the list sees the request first and the response last.
Question 4Medium
A new-style middleware class defines __call__(self, request). What is its typical structure?
- It must always return None to continue
- Code before get_response, then code after it
- It must subclass HttpResponse to be valid
- It runs only once when the server starts
Answer: B. Code before get_response, then code after it
Explanation
__call__ calls self.get_response and can modify both sides.
Step-by-step solution
- A new-style middleware is a callable class.
- Call receives the request and calls self.get_response to run the rest of the chain and the view.
- Code before get_response handles the request; code after it handles the response.
- So the answer is option B.
Why the other options are wrong
- A. It returns the response from get_response; it does not return None to continue.
- C. The class does not need to subclass HttpResponse.
- D. Call runs for each request, not once at server start.
Tip: Middleware shape: work before get_response for the request, work after it for the response.
Question 5Medium
Which middleware method is called when a view raises an exception that is not handled?
- process_template_response()
- process_view()
- __del__()
- process_exception()
Answer: D. process_exception()
Explanation
When a view raises an exception, Django calls process_exception(request, exception) on the middleware. If it returns a response, Django uses that response.
Step-by-step solution
- The view raises an exception.
- Django looks for process_exception on the middleware.
- If a middleware returns an HttpResponse, that response is used.
Why the other options are wrong
- A. That hook runs after a view returns a template response, not when it raises.
- B. process_view runs before the view is called, so it cannot catch an error the view raises.
- C. __del__ is Python's destructor, not a Django middleware hook.
Tip: Return None from process_exception to let Django continue with its normal error handling.
Question 6Medium
In a new-style middleware class, what is the get_response argument passed to __init__?
- The response that was returned by the previous request
- A callable that hands the request to the next layer
- A dictionary of all the request headers and cookies
- The URL pattern that matched the request, as a string
Answer: B. A callable that hands the request to the next layer
Explanation
Django builds the middleware chain by passing each middleware the next callable. Calling get_response(request) runs the rest of the chain and returns the response.
Step-by-step solution
- Django passes get_response into __init__.
- Calling get_response(request) runs the next layer.
- The result is the response, which the middleware can change.
Why the other options are wrong
- A. Each request is handled fresh; get_response is a callable, not a stored response.
- C. Headers are read from the request object, not from get_response.
- D. URL matching is done by the URL resolver, not passed to middleware this way.
Tip: Call get_response(request) once, unless you want to short-circuit the chain.
Question 7Hard
Which middleware method is called just before Django calls the view (new-style classes)?
- process_request()
- process_view()
- __init__()
- process_template()
Answer: B. process_view()
Explanation
process_view receives the view function and its args.
Step-by-step solution
- Middleware methods run at different points in a request.
- process_view() runs after URL resolution, just before the view is called.
- It receives the view function and its arguments.
- So the answer is option B, process_view().
Why the other options are wrong
- A.
process_request() is an old-style hook that runs before the URL is resolved, not just before the view. - C.
__init__() runs once when the middleware is created, not per request. - D.
process_template() is not a middleware method.
Tip: Request, then process_view, then the view, then the response.
Question 8Hard
A middleware's __call__ returns an HttpResponse without calling get_response. What happens?
- The view and later middleware are skipped; the response goes back out
- Django raises an error because get_response was not called
- The view runs first and then the response is replaced
- Only this middleware is skipped; the view still runs
Answer: A. The view and later middleware are skipped; the response goes back out
Explanation
Calling get_response is what passes the request inward. If a middleware returns a response first, the inner layers and the view are skipped. The response still passes back out through the outer middleware.
Step-by-step solution
- __call__ receives the request.
- It returns a response without calling get_response.
- The inner middleware and the view never run.
- The response still passes back through the outer middleware.
Why the other options are wrong
- B. Not calling get_response is allowed; it is how a middleware short-circuits the chain.
- C. The view never runs in this case, because get_response was not called.
- D. Skipping get_response also skips everything after this middleware, including the view.
Tip: Use this to block requests early, for example for a maintenance page or a rate limit.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →