Skip to content

Instantly share code, notes, and snippets.

@richellyitalo
Created March 31, 2019 23:33
Show Gist options
  • Save richellyitalo/52a2d71afd5d65678cb268c29b181d33 to your computer and use it in GitHub Desktop.
Save richellyitalo/52a2d71afd5d65678cb268c29b181d33 to your computer and use it in GitHub Desktop.

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
    ),
}

Adiciona o authtoken

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/
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment