Middleware & Settings Questions with Answers

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

Sample questions

Question 1Easy

Which command gathers static files to STATIC_ROOT for production?

  1. python manage.py collectstatic
  2. python manage.py staticfiles
  3. python manage.py copystatic
  4. python manage.py buildstatic

Answer: A. python manage.py collectstatic

Explanation

A web server such as Nginx then serves STATIC_ROOT.

Step-by-step solution

  1. Run collectstatic.
  2. Serve folder.

Why the other options are wrong

  • B. python manage.py staticfiles is not a Django management command; the correct choice is python manage.py collectstatic.
  • C. python manage.py copystatic is not a Django management command; the correct choice is python manage.py collectstatic.
  • D. python manage.py buildstatic is not a Django management command; the correct choice is python manage.py collectstatic.

Question 2Easy

Which setting lists the hostnames Django will serve when DEBUG is False?

  1. INSTALLED_HOSTS
  2. DOMAINS
  3. SERVER_NAMES
  4. ALLOWED_HOSTS

Answer: D. ALLOWED_HOSTS

Explanation

A mismatching Host header returns 400.

Step-by-step solution

  1. Set list.
  2. Others rejected.

Why the other options are wrong

  • A. INSTALLED_HOSTS is not a Django setting; the correct choice is ALLOWED_HOSTS.
  • B. DOMAINS is not a Django setting; the correct choice is ALLOWED_HOSTS.
  • C. SERVER_NAMES is not a Django setting; the correct choice is ALLOWED_HOSTS.

Question 3Easy

Where is the list of middleware configured?

  1. MIDDLEWARE in settings.py
  2. urls.py
  3. models.py
  4. wsgi.py

Answer: A. MIDDLEWARE in settings.py

Explanation

Each entry is a dotted path to a middleware class or callable.

Step-by-step solution

  1. Edit MIDDLEWARE.
  2. Order matters.

Why the other options are wrong

  • B. URLs map views.
  • C. Models are data.
  • D. wsgi exposes the app.

Question 4Medium

In what order does middleware process a request and its response?

  1. Both top to bottom, so the response passes through the same order as the request
  2. Requests top to bottom; responses bottom to top
  3. Both bottom to top
  4. Random

Answer: B. Requests top to bottom; responses bottom to top

Explanation

Like layers of an onion around the view.

Step-by-step solution

  1. Request goes in.
  2. 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?

  1. A settings.py file committed in the Git repo so every developer has the same values
  2. Environment variables or a secrets manager, not committed code
  3. A comment in urls.py
  4. 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

  1. Read from env.
  2. 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?

  1. Returns the rendered template directly, so the view and later layers are skipped
  2. Calls the view directly
  3. Calls get_response(request) to pass control onward, then can modify the response
  4. 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

  1. Code in __call__ before get_response(request) runs on the request.
  2. get_response(request) passes control to the next layer (the next middleware or the view) and returns its response.
  3. 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.

Practise all 6 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