Configura o rest em 'settings.py'
REST_FRAMEWORK = {
# ...
'DEFAULT_PERMISSION_CLASSES' : (
'rest_framework.permissions.IsAuthenticated' ,
),
'DEFAULT_AUTHENTICATION_CLASSES' : (
'rest_framework.authentication.SessionAuthentication' ,
'rest_framework.authentication.TokenAuthentication' , # Necessário adicionar 'rest_framework.authtoken' ao INSTALLED_APPS
),
}
INSTALLED_APPS = [
# ...
'rest_framework' ,
'rest_framework.authtoken'
]
URL de solicitação de token
from django .contrib import admin
from django .urls import path , include
from django .conf .urls import url
from rest_framework import routers
from rest_framework .authtoken import views
from api .viewsets import CategoryViewSet , ProductViewSet
router = routers .DefaultRouter (trailing_slash = False )
router .register (r'categories' , CategoryViewSet )
router .register (r'products' , ProductViewSet )
urlpatterns = [
path ('admin/' , admin .site .urls ),
path ('api/' , include (router .urls + [url ('auth' , views .obtain_auth_token )])),
path ('api-auth/' , include ('rest_framework.urls' , namespace = 'rest_framework' )) # Protege a área de api/
]