0
0
Djangoframework~10 mins

Request/response middleware flow in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a middleware class with the correct method to process requests.

Django
class SimpleMiddleware:
    def [1](self, request):
        print("Request received")
        return None
Drag options to blanks, or click blank then click option'
Ahandle_request
Bprocess_request
Crequest_process
Dprocess_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using process_response instead of process_request for request handling.
Using incorrect method names like handle_request.
2fill in blank
medium

Complete the code to define a middleware method that modifies the response before returning it.

Django
class SimpleMiddleware:
    def [1](self, request, response):
        response['X-Custom-Header'] = 'Value'
        return response
Drag options to blanks, or click blank then click option'
Amodify_response
Bprocess_request
Chandle_response
Dprocess_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using process_request to modify responses.
Using non-standard method names like handle_response.
3fill in blank
hard

Fix the error in the middleware method name that should process the request.

Django
class CustomMiddleware:
    def [1](self, request):
        print("Processing request")
        return None
Drag options to blanks, or click blank then click option'
Aprocess_request
Brequest_handler
Chandle_request
Dprocess_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using process_response instead of process_request.
Using custom method names not recognized by Django.
4fill in blank
hard

Fill both blanks to create a middleware that processes request and response correctly.

Django
class LoggingMiddleware:
    def [1](self, request):
        print("Request logged")
        return None

    def [2](self, request, response):
        print("Response logged")
        return response
Drag options to blanks, or click blank then click option'
Aprocess_request
Bhandle_request
Cprocess_response
Dhandle_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using handle_request or handle_response which are not recognized.
Mixing up request and response method names.
5fill in blank
hard

Fill all three blanks to complete a middleware that logs request path, processes request, and adds a header to response.

Django
class HeaderMiddleware:
    def [1](self, request):
        print(f"Path: {request.[2]")
        return None

    def [3](self, request, response):
        response['X-Processed'] = 'True'
        return response
Drag options to blanks, or click blank then click option'
Aprocess_request
Bpath
Cprocess_response
Durl
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.url which does not exist in Django request.
Using incorrect method names for middleware.