Middleware Questions with Answers

8 practice questions on Middleware (Django) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

Where do you list the middleware classes used by a Django project?

  1. In urls.py, next to the URL patterns for each view
  2. In manage.py, which runs management commands from the shell
  3. In settings.py, in the MIDDLEWARE list
  4. 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

  1. Project settings live in settings.py.
  2. MIDDLEWARE is a list of dotted class paths.
  3. 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?

  1. Before the URL is matched, while Django still resolves the path
  2. Only for 404 pages, when no view matches the requested URL
  3. After a view returns a TemplateResponse, before it is rendered
  4. 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

  1. The view returns a TemplateResponse.
  2. The response has a render() method but is not rendered yet.
  3. Django calls process_template_response on the middleware.
  4. 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?

  1. Bottom to top for both the request and the response
  2. Top to bottom for both the request and the response
  3. Down the list for requests, up for responses
  4. 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

  1. Middleware wraps the view like layers of an onion.
  2. On a request, Django runs the MIDDLEWARE list from the top down.
  3. On the response, the same middleware runs back up the list.
  4. 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?

  1. It must always return None to continue
  2. Code before get_response, then code after it
  3. It must subclass HttpResponse to be valid
  4. 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

  1. A new-style middleware is a callable class.
  2. Call receives the request and calls self.get_response to run the rest of the chain and the view.
  3. Code before get_response handles the request; code after it handles the response.
  4. 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?

  1. process_template_response()
  2. process_view()
  3. __del__()
  4. 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

  1. The view raises an exception.
  2. Django looks for process_exception on the middleware.
  3. 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__?

  1. The response that was returned by the previous request
  2. A callable that hands the request to the next layer
  3. A dictionary of all the request headers and cookies
  4. 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

  1. Django passes get_response into __init__.
  2. Calling get_response(request) runs the next layer.
  3. 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)?

  1. process_request()
  2. process_view()
  3. __init__()
  4. process_template()

Answer: B. process_view()

Explanation

process_view receives the view function and its args.

Step-by-step solution

  1. Middleware methods run at different points in a request.
  2. process_view() runs after URL resolution, just before the view is called.
  3. It receives the view function and its arguments.
  4. 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?

  1. The view and later middleware are skipped; the response goes back out
  2. Django raises an error because get_response was not called
  3. The view runs first and then the response is replaced
  4. 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

  1. __call__ receives the request.
  2. It returns a response without calling get_response.
  3. The inner middleware and the view never run.
  4. 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 →

Related topics

See all Django topics · All topics