django-rest-framework Token Authentication With Django Rest Framework USING THE TOKEN


Let’s create a new View that requires this authentication mechanism.

We need to add these import lines:

from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated

and then create the new View in the same file

class AuthView(APIView):
    Authentication is needed for this methods
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)
    def get(self, request, format=None):
        return Response({'detail': "I suppose you are authenticated"})

As we did on previous post, we need to tell our project that we have a new REST path listening, on test_app/

from rest_framework.urlpatterns import format_suffix_patterns
from test_app import views
urlpatterns = patterns('test_app.views',
    url(r'^', views.TestView.as_view(), name='test-view'),
    url(r'^auth/', views.AuthView.as_view(), name='auth-view'),
urlpatterns = format_suffix_patterns(urlpatterns)