๐Ÿ“ Django

Token Authentication in DRF

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
258
๐ŸŒณ
Level
Advanced

Covered topics: Setup, Endpoint for Obtaining a Token, Using the Token, Creating a Token for a User.

Setup

INSTALLED_APPS = [
    ...,
    'rest_framework',
    'rest_framework.authtoken',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}
python manage.py migrate

Endpoint for Obtaining a Token

# urls.py
from rest_framework.authtoken.views import obtain_auth_token

urlpatterns = [
    path('api/token/', obtain_auth_token, name='api-token'),
]
# Obtain a token
curl -X POST http://localhost:8000/api/token/ \
  -H "Content-Type: application/json" \
  -d '{"username": "user", "password": "pass"}'

# Response
{"token": "9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b"}

The built-in token implementation is intentionally simple: tokens do not expire automatically. In production, expose authentication only over HTTPS and add throttling to the token endpoint.

Using the Token

curl http://localhost:8000/api/tasks/ \
  -H "Authorization: Token replace-with-your-token"

Creating a Token for a User

from rest_framework.authtoken.models import Token

# On user creation
token = Token.objects.create(user=user)

# Via signal (automatically)
from django.db.models.signals import post_save
from django.contrib.auth.models import User

@receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

Managing Tokens in the Admin

# Available automatically after migrate
# /admin/authtoken/token/

Custom Endpoint with User Data

from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny

@api_view(['POST'])
@permission_classes([AllowAny])
def login_view(request):
    username = request.data.get('username')
    password = request.data.get('password')
    user = authenticate(username=username, password=password)
    if user:
        token, _ = Token.objects.get_or_create(user=user)
        return Response({'token': token.key, 'user_id': user.pk})
    return Response({'error': 'Invalid credentials'}, status=401)

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Authentication in DRF

DRF supports several authentication methods. The right choice depends on the client type, threat model,...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 282
๐Ÿ“

Authentication in Django

Django includes a complete authentication system: the User model, login/logout, and permissions.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 281
๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 278

Did you like the article?

Subscribe to our updates and be the first to receive new articles. Grow with PyLand!