← Back to Django Course | Chapter 14: REST API with DRF | Lesson 6 of 6

DRF Authentication Basics

DRF authentication checks who is making a request, so private data isn't handed out to just anyone.

Authentication vs Permissions

Authentication answers 'who is this?' (e.g. checking a session cookie or token). Permissions answer 'what can this user do?' (e.g. only authenticated users may create a book). DRF separates the two so they can be mixed and matched.

Example: Authentication vs Permissions

Authentication answers 'who is this?' (e.g. checking a session cookie or token). Permissions answer 'what can this user do?' (e.g. only authenticated users may create a book). DRF separates the two so they can be mixed and matched.

markup
# Authentication: identifies the user (or None for anonymous)
# Permissions: decides whether that user may perform this action
#
# request.user  -> set by authentication
# IsAuthenticated, AllowAny, IsAdminUser -> examples of permissions
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Requiring Authentication on a View

permission_classes on an APIView restricts access; IsAuthenticated rejects any request that isn't logged in with a 401/403 response.

Example: Requiring Authentication on a View

permission_classes on an APIView restricts access; IsAuthenticated rejects any request that isn't logged in with a 401/403 response.

markup
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

class BookListView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        return Response({'message': f'Hello {request.user.username}'})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Project-Wide Defaults

Instead of repeating permission_classes on every view, set defaults once in settings.py so all API views require authentication unless a view explicitly overrides it.

Example: Project-Wide Defaults

Instead of repeating permission_classes on every view, set defaults once in settings.py so all API views require authentication unless a view explicitly overrides it.

markup
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Enabling a permission class like IsAuthenticated without configuring any authentication class, so every request is rejected because DRF has no way to identify the user.
  2. Assuming Django's session login automatically protects API endpoints, when DRF views need their own authentication_classes/permission_classes configured.
  3. Hardcoding permissions per view instead of setting sensible project-wide defaults in settings.py, leading to inconsistent access rules across endpoints.
Chapter Summary
  • Authentication identifies who is making a request; permissions decide what that identified user is allowed to do.
  • DRF supports multiple authentication schemes, including SessionAuthentication (reuses Django's login) and TokenAuthentication (API tokens).
  • DEFAULT_AUTHENTICATION_CLASSES and DEFAULT_PERMISSION_CLASSES in settings.py set project-wide defaults for every API view.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.