📖 Scenario: You are building a Django web application that needs to log every request's path for debugging purposes.
🎯 Goal: Create a custom middleware that logs the request path, then add it to the Django project's middleware settings.
📋 What You'll Learn
Create a custom middleware class named LogRequestMiddleware in a file called middleware.py inside an app named core.
The middleware must have a __call__ method that logs the request path using print().
Add the core.middleware.LogRequestMiddleware to the MIDDLEWARE list in settings.py after django.middleware.common.CommonMiddleware.
💡 Why This Matters
🌍 Real World
Middleware is used in Django projects to add functionality that runs on every request, such as logging, authentication, or security checks.
💼 Career
Understanding middleware configuration is essential for backend developers working with Django to customize request handling and add cross-cutting features.
Progress0 / 4 steps
1
Create the middleware class
In the core/middleware.py file, create a class called LogRequestMiddleware with an __init__ method that accepts get_response and stores it as an instance variable.
Django
Hint
Middleware classes in Django need an __init__ method that takes get_response and saves it.
2
Add the __call__ method to log request path
Add a __call__ method to LogRequestMiddleware that takes request, prints request.path, then returns the response by calling self.get_response(request).
Django
Hint
The __call__ method processes each request. Use print(request.path) to log the path.
3
Open settings.py and locate MIDDLEWARE list
Open settings.py and find the MIDDLEWARE list variable. Do not change it yet.
Django
Hint
The MIDDLEWARE list is usually near the top of settings.py.
4
Add custom middleware to MIDDLEWARE list
Add the string 'core.middleware.LogRequestMiddleware' to the MIDDLEWARE list in settings.py immediately after 'django.middleware.common.CommonMiddleware'.
Django
Hint
Insert your middleware string right after 'django.middleware.common.CommonMiddleware' in the list.
Practice
(1/5)
1. What is the purpose of middleware in a Django project?
easy
A. To create HTML templates
B. To process requests and responses globally before reaching views
C. To define database models
D. To manage user sessions only
Solution
Step 1: Understand middleware role
Middleware runs code on every request and response, acting globally.
Step 2: Compare with other options
Models define data, templates create HTML, sessions are part of middleware but not the only role.
Final Answer:
To process requests and responses globally before reaching views -> Option B
Quick Check:
Middleware = global request/response processing [OK]
Hint: Middleware acts on all requests and responses [OK]
Common Mistakes:
Confusing middleware with models or templates
Thinking middleware only manages sessions
Assuming middleware runs only after views
2. Which of the following is the correct way to add a middleware class in Django's settings.py?
easy
A. 'django.middleware.security.SecurityMiddleware',
B. django.middleware.security.SecurityMiddleware
C. 'SecurityMiddleware',
D. SecurityMiddleware
Solution
Step 1: Check syntax for middleware entry
Middleware classes must be strings with full Python path and comma-separated in list.
Step 2: Validate options
'django.middleware.security.SecurityMiddleware', is a string with full path and comma, correct syntax. Others miss quotes or full path.
Final Answer:
'django.middleware.security.SecurityMiddleware', -> Option A
Quick Check:
Middleware entries are full strings with commas [OK]
Hint: Middleware must be full string path with quotes and comma [OK]
Which middleware runs first on an incoming request?
medium
A. SessionMiddleware
B. CommonMiddleware
C. SecurityMiddleware
D. All run simultaneously
Solution
Step 1: Understand middleware order on request
Middleware runs in the order listed for requests, first listed runs first.
Step 2: Identify first middleware in list
SecurityMiddleware is first, so it runs first on incoming requests.
Final Answer:
SecurityMiddleware -> Option C
Quick Check:
Request middleware order = list order [OK]
Hint: First middleware in list runs first on requests [OK]
Common Mistakes:
Thinking middleware runs in reverse order on requests
Assuming all middleware run at the same time
Confusing request and response middleware order
4. You added a custom middleware class to MIDDLEWARE but Django raises an error on startup. What is a common cause?
medium
A. Middleware class is not a string in MIDDLEWARE list
B. Middleware class missing required methods like __call__ or __init__
C. Middleware class is listed after django.middleware.common.CommonMiddleware
D. Middleware class is defined inside views.py
Solution
Step 1: Check middleware list format
Middleware entries must be strings with full Python path, not class objects.
Step 2: Identify common mistake
Adding the class object instead of string path causes Django startup error.
Final Answer:
Middleware class is not a string in MIDDLEWARE list -> Option A
Quick Check:
MIDDLEWARE entries must be strings [OK]
Hint: Middleware list must contain strings, not class objects [OK]
Common Mistakes:
Adding middleware as class instead of string path
Placing middleware in wrong order causing errors
Defining middleware in wrong file without import
5. You want to add a middleware that modifies the response headers after all other middleware have processed the response. Where should you place it in the MIDDLEWARE list?
hard
A. At the end of the list
B. Right before django.middleware.common.CommonMiddleware
C. Anywhere, order does not matter for response
D. At the beginning of the list
Solution
Step 1: Understand middleware response order
Middleware processes responses in reverse order of the list.
Step 2: Place middleware for last response processing
To modify response after all other middleware (run last on response phase), place at beginning of list since first in list runs last on response.
Final Answer:
At the beginning of the list -> Option D
Quick Check:
Response middleware order = reverse list order [OK]
Hint: First in list runs last on response, put first for final changes [OK]
Common Mistakes:
Placing middleware at end expecting last response run
Assuming request and response order are the same
Ignoring middleware order effects on response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.