0
0
Djangoframework~10 mins

DRF authentication (Token, JWT) 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 TokenAuthentication class from DRF.

Django
from rest_framework.authentication import [1]
Drag options to blanks, or click blank then click option'
ATokenAuthentication
BSessionAuthentication
CBasicAuthentication
DJWTAuthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Importing SessionAuthentication instead of TokenAuthentication
Trying to import JWTAuthentication which is not in rest_framework.authentication
2fill in blank
medium

Complete the code to add TokenAuthentication to the default authentication classes in DRF settings.

Django
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.[1]',
    ]
}
Drag options to blanks, or click blank then click option'
ASessionAuthentication
BTokenAuthentication
CBasicAuthentication
DJWTAuthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Using SessionAuthentication instead of TokenAuthentication
Using JWTAuthentication string which is not built-in in DRF
3fill in blank
hard

Fix the error in the code to import the JWTAuthentication class from the djangorestframework_simplejwt package.

Django
from rest_framework_simplejwt.authentication import [1]
Drag options to blanks, or click blank then click option'
AJWTAuthentication
BTokenAuthentication
CSimpleJWTAuthentication
DBasicAuthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Importing TokenAuthentication from simplejwt package
Using a wrong class name like SimpleJWTAuthentication
4fill in blank
hard

Fill both blanks to configure JWTAuthentication and TokenAuthentication in DRF settings.

Django
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.[1]',
        'rest_framework.authentication.[2]',
    ]
}
Drag options to blanks, or click blank then click option'
AJWTAuthentication
BTokenAuthentication
CSessionAuthentication
DBasicAuthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of authentication classes
Using SessionAuthentication instead of TokenAuthentication
5fill in blank
hard

Fill all three blanks to create a view that uses JWTAuthentication and requires the user to be authenticated.

Django
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import [1]
from rest_framework_simplejwt.authentication import [2]

class MyView(APIView):
    authentication_classes = [[3]]
    permission_classes = [IsAuthenticated]

    def get(self, request):
        return Response({'message': 'Hello, authenticated user!'})
Drag options to blanks, or click blank then click option'
AIsAuthenticated
BJWTAuthentication
CJWTAuthentication()
DTokenAuthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Using JWTAuthentication() with parentheses in authentication_classes
Forgetting to import IsAuthenticated permission