MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Token Authentication0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
4.1Token Authentication4.2JWT Authentication4.3Custom Permissions
HomeTutorialsREST API Development with DjangoToken Authentication
JWT Authentication

Token Authentication

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Securing API endpoints requires identifying the caller on every request. Django REST Framework ships with built-in authentication mechanisms, the two most prominent being SessionAuthentication (ideal for browser web SPAs on the same domain) and TokenAuthentication (ideal for mobile apps, external clients, and third-party integrations).


1. SessionAuthentication vs TokenAuthentication

FeatureSessionAuthenticationTokenAuthentication
CredentialCookie-based session ID (sessionid)Static Bearer Token string
CSRF Required?Yes (on all mutating requests)No (immune to CSRF)
Best Used ForSame-domain React/Vue SPAsMobile apps, CLI tools, server-to-server APIs
Storagedjango_session tableauthtoken_token table

2. Setting Up DRF TokenAuthentication

Add rest_framework.authtoken to INSTALLED_APPS:

1# config/settings.py
2INSTALLED_APPS = [
3 # ...
4 "rest_framework",
5 "rest_framework.authtoken", # Creates authtoken_token database table
6]
7 
8# Run migration to create token table
9# python manage.py migrate

Configure TokenAuthentication in global settings:

1REST_FRAMEWORK = {
2 "DEFAULT_AUTHENTICATION_CLASSES": [
3 "rest_framework.authentication.TokenAuthentication",
4 "rest_framework.authentication.SessionAuthentication",
5 ],
6}

3. Exposing the Login / Token Obtain Endpoint

DRF provides a built-in view (obtain_auth_token) that validates username and password and returns a token:

1# config/urls.py
2from django.urls import path, include
3from rest_framework.authtoken.views import obtain_auth_token
4 
5urlpatterns = [
6 # POST /api/auth/token/ { "username": "...", "password": "..." }
7 path("api/auth/token/", obtain_auth_token, name="api-token-auth"),
8]

Response:

1{
2 "token": "9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b"
3}

4. Making Authenticated Requests

Clients transmit the token in the Authorization HTTP header prefixed with Token:

1GET /api/v1/projects/ HTTP/1.1
2Host: api.enterprise.com
3Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b
4Accept: application/json

5. Automatically Generating Tokens on User Signup

Use a Django post_save signal to automatically generate an auth token whenever a new user registers:

1# accounts/signals.py
2from django.conf import settings
3from django.db.models.signals import post_save
4from django.dispatch import receiver
5from rest_framework.authtoken.models import Token
6 
7@receiver(post_save, sender=settings.AUTH_USER_MODEL)
8def create_auth_token(sender, instance=None, created=False, **kwargs):
9 if created:
10 Token.objects.create(user=instance)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What HTTP header and prefix format does DRF TokenAuthentication expect?
Question 2
Why does TokenAuthentication not require CSRF token validation on POST requests?
Question 3
What database table is created by adding rest_framework.authtoken to INSTALLED_APPS?
Question 4
What view does DRF provide out-of-the-box to exchange credentials for an auth token?
Question 5
What is a known limitation of DRF's built-in TokenAuthentication in large-scale enterprise systems?
Next Lesson

JWT Authentication

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
URL RoutingJWT Authentication

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext