Token Authentication Example
A snippet showing how to enable token authentication in DRF.
# settings.py
INSTALLED_APPS += ["rest_framework.authtoken"]
# Create tokens table
python manage.py migrate
# settings.py (REST_FRAMEWORK)
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
}
# urls.py - obtain token
from django.urls import path
from rest_framework.authtoken.views import obtain_auth_token
urlpatterns = [
path("api/token/", obtain_auth_token, name="api-token"),
]
Explanation:
-
POST username/password to /api/token/ to receive an auth token; include in 'Authorization: Token
'.
- Category Django REST Framework (DRF)
- Total Views 617
- Last Modified 12 June, 2026
- Tags #drf #token #authentication #api
Previous snippet
Router Example
Next snippet