0
0
Djangoframework~10 mins

Why middleware matters in Django - Test Your Understanding

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

Complete the code to add a middleware class to the Django settings.

Django
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    [1],
    'django.middleware.common.CommonMiddleware',
]
Drag options to blanks, or click blank then click option'
A'django.middleware.csrf.CsrfViewMiddleware'
B'django.middleware.session.SessionMiddleware'
C'django.middleware.clickjacking.XFrameOptionsMiddleware'
D'django.middleware.locale.LocaleMiddleware'
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing middleware unrelated to sessions.
Forgetting to include middleware that manages security.
2fill in blank
medium

Complete the middleware method to process a request in a custom middleware class.

Django
class CustomMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def [1](self, request):
        # Code to run before view
        response = self.get_response(request)
        # Code to run after view
        return response
Drag options to blanks, or click blank then click option'
A__call__
Bprocess_request
Cprocess_response
Dprocess_view
Attempts:
3 left
💡 Hint
Common Mistakes
Using old-style middleware methods like process_request.
Not defining the __call__ method in new middleware.
3fill in blank
hard

Fix the error in the middleware to correctly modify the response headers.

Django
class HeaderMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        response.[1]['X-Custom-Header'] = 'Value'
        return response
Drag options to blanks, or click blank then click option'
Aadd_header
B__setitem__
Cheaders
DMETA
Attempts:
3 left
💡 Hint
Common Mistakes
Using META which is for request headers, not response.
Trying to call a method instead of accessing the headers attribute.
4fill in blank
hard

Fill both blanks to create a middleware that blocks requests from a specific IP address.

Django
class BlockIPMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if request.META.get('[1]') == '[2]':
            from django.http import HttpResponseForbidden
            return HttpResponseForbidden('Forbidden')
        return self.get_response(request)
Drag options to blanks, or click blank then click option'
AREMOTE_ADDR
BHTTP_USER_AGENT
C192.168.1.1
D127.0.0.1
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP_USER_AGENT instead of REMOTE_ADDR.
Blocking an IP address that is not the one intended.
5fill in blank
hard

Fill all three blanks to write a middleware that adds a custom header only for authenticated users.

Django
class AuthHeaderMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        if request.[1].[2]:
            response.[3]['X-User-Status'] = 'Authenticated'
        return response
Drag options to blanks, or click blank then click option'
Auser
Bis_authenticated
Cheaders
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.session instead of request.user.
Trying to set headers directly without using the headers attribute.