0
0
Djangoframework~10 mins

Authentication middleware 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 import the Django middleware base class.

Django
from django.utils.deprecation import [1]
Drag options to blanks, or click blank then click option'
AMiddlewareMixin
BMiddlewareBase
CMiddlewareHandler
DMiddlewareCore
Attempts:
3 left
💡 Hint
Common Mistakes
Using MiddlewareBase which does not exist in Django.
Confusing MiddlewareMixin with other middleware classes.
2fill in blank
medium

Complete the method name to process a request in middleware.

Django
def [1](self, request):
    # Your code here
    return None
Drag options to blanks, or click blank then click option'
Aprocess_view
Bprocess_response
Cprocess_exception
Dprocess_request
Attempts:
3 left
💡 Hint
Common Mistakes
Using process_response which runs after the view.
Using process_exception which handles errors.
3fill in blank
hard

Fix the error in the middleware class definition by completing the base class.

Django
class AuthMiddleware([1]):
    def process_request(self, request):
        if not request.user.is_authenticated:
            # Redirect or deny access
            pass
Drag options to blanks, or click blank then click option'
AMiddlewareMixin
BMiddlewareBase
Cobject
DBaseMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Inheriting from object which does not provide middleware features.
Using non-existent base classes.
4fill in blank
hard

Fill both blanks to check if the user is authenticated and redirect if not.

Django
def process_request(self, request):
    if not request.user.[1]:
        return [2]('/login/')
Drag options to blanks, or click blank then click option'
Ais_authenticated
Bredirect
CHttpResponseRedirect
Dis_active
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_active instead of is_authenticated.
Using redirect function without importing HttpResponseRedirect.
5fill in blank
hard

Fill all three blanks to import HttpResponseRedirect, define middleware class, and implement process_request.

Django
from django.http import [1]

class [2]([3]):
    def process_request(self, request):
        if not request.user.is_authenticated:
            return HttpResponseRedirect('/login/')
Drag options to blanks, or click blank then click option'
AHttpResponseRedirect
BAuthMiddleware
CMiddlewareMixin
DHttpRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to import HttpResponseRedirect.
Not inheriting from MiddlewareMixin.
Incorrect class name.