Complete the code to define a middleware class with the correct method to process requests.
class SimpleMiddleware: def [1](self, request): print("Request received") return None
process_response instead of process_request for request handling.handle_request.The process_request method is used in Django middleware to handle incoming requests before the view is called.
Complete the code to define a middleware method that modifies the response before returning it.
class SimpleMiddleware: def [1](self, request, response): response['X-Custom-Header'] = 'Value' return response
process_request to modify responses.handle_response.The process_response method is called after the view returns a response, allowing middleware to modify it.
Fix the error in the middleware method name that should process the request.
class CustomMiddleware: def [1](self, request): print("Processing request") return None
process_response instead of process_request.The correct method name to process requests in Django middleware is process_request. Other names will not be called by Django.
Fill both blanks to create a middleware that processes request and response correctly.
class LoggingMiddleware: def [1](self, request): print("Request logged") return None def [2](self, request, response): print("Response logged") return response
handle_request or handle_response which are not recognized.The middleware uses process_request to handle requests and process_response to handle responses, following Django's middleware pattern.
Fill all three blanks to complete a middleware that logs request path, processes request, and adds a header to response.
class HeaderMiddleware: def [1](self, request): print(f"Path: {request.[2]") return None def [3](self, request, response): response['X-Processed'] = 'True' return response
request.url which does not exist in Django request.The middleware uses process_request to log the request path accessed via request.path, and process_response to add a custom header to the response.