How to route an url to an specific method of a class Django and DRF
Question: I am very new in the python world and now I building an application with
Django 1.8 with the Rest Framework and I want to create a class view to DRY my
code.

For example I want to have a class view for the students in my system

    
    
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import status
    
    
    class SnippetList(APIView):
        def getWorstStudents(self, request, format=None):
            # Logic here
    

How can I assign a specified URL in **urls.py** to hit this method?

And also I have implemented **REST framework JWT Auth**
<http://getblimp.github.io/django-rest-framework-jwt/> for token auth.

How can I restrict the access to allow only authenticated users can access to
this url?

Thank you in advance!


Answer: You can set urls like any other Django app, [documented
here](https://docs.djangoproject.com/en/1.8/topics/http/urls/#example)

    
    
    # urls.py
    from django.conf.urls import url
    from somewhere import SnippetList
    
    urlpatterns = [
        url(r'^your/url/$', SnippetList.as_view()),
    ]
    

About DRY with your method, you can define the method you want to response to,
and call the `getWorstStudents` (btw by convention I would call it
get_worst_students). Let's say you want to response the `post` method:

    
    
    # views.py
    
    from rest_framework.response import Response
    
    def getWorstStudents(params)
    
    class SnippetList(APIView):
        def post(self, request, *args, **kwargs):
            # call getWorstStudents method here and response a Response Object
    

You can define `getWorstStudents` inside `SnippetList` class or in other file
to import wherever you need it.

Finally, about authentication, DRF provides classes for this, [documented
here](http://www.django-rest-framework.org/api-guide/authentication/).

From docs, you need define this in your `settings.py` file:

    
    
    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
        )
    }
    

And use it in your views:

    
    
    from rest_framework.authentication import SessionAuthentication, BasicAuthentication
    from rest_framework.permissions import IsAuthenticated
    from rest_framework.response import Response
    from rest_framework.views import APIView
    
    class ExampleView(APIView):
        authentication_classes = (SessionAuthentication, BasicAuthentication)
        permission_classes = (IsAuthenticated,)
    
        def get(self, request, format=None):
            content = {
                'user': unicode(request.user),  # `django.contrib.auth.User` instance.
                'auth': unicode(request.auth),  # None
            }
            return Response(content)
    

You can also define your own authentication class and set it in
`authentication_classes` tuple. [Custom authentication classes documented
here.](http://www.django-rest-framework.org/api-guide/authentication/#custom-
authentication)

