Working with routing and endpoints

We want to create an endpoint for the root of our web service to make it easy to browse the resource collections and resources provided by our web service with the browsable API feature and understand how everything works. Add the following code to the views.py file in the restful01/drones folder to declare the ApiRoot class as a subclass of the generics.GenericAPIView class. The code file for the sample is included in the hillar_django_restful_06_01 folder in the restful01/drones/views.py file:

class ApiRoot(generics.GenericAPIView): 
    name = 'api-root' 
    def get(self, request, *args, **kwargs): 
        return Response({ 
            'drone-categories': reverse(DroneCategoryList.name, request=request), 
            'drones': reverse(DroneList.name, request=request), 
            'pilots': reverse(PilotList.name, request=request), 
            'competitions': reverse(CompetitionList.name, request=request) 
            }) 

The ApiRoot class is a subclass of the rest_framework.generics.GenericAPIView class and declares the get method. The GenericAPIView class is the base class for all the other generic views we have previousy analyzed.

The ApiRoot class defines the get method that returns a Response object with key/value pairs of strings that provide a descriptive name for the view and its URL, generated with the rest_framework.reverse.reverse function. This URL resolver function returns a fully qualified URL for the view.

Go to the restful01/drones folder and create a new file named urls.py. Write the following code in this new file. The following lines show the code for this file that defines the URL patterns that specifies the regular expressions that have to be matched in the request to run a specific method for a class-based view defined in the views.py file.

In the previous example, we specified a function that represented a view. In this case, we call the as_view method for each appropriate class-based view. The code file for the sample is included in the hillar_django_restful_06_01 folder in the restful01/drones/urls.py file:

from django.conf.urls import url 
from drones import views 
 
 
urlpatterns = [ 
    url(r'^drone-categories/$',  
        views.DroneCategoryList.as_view(),  
        name=views.DroneCategoryList.name), 
    url(r'^drone-categories/(?P<pk>[0-9]+)$',  
        views.DroneCategoryDetail.as_view(), 
        name=views.DroneCategoryDetail.name), 
    url(r'^drones/$',  
        views.DroneList.as_view(), 
        name=views.DroneList.name), 
    url(r'^drones/(?P<pk>[0-9]+)$',  
        views.DroneDetail.as_view(), 
        name=views.DroneDetail.name), 
    url(r'^pilots/$',  
        views.PilotList.as_view(), 
        name=views.PilotList.name), 
    url(r'^pilots/(?P<pk>[0-9]+)$',  
        views.PilotDetail.as_view(), 
        name=views.PilotDetail.name), 
    url(r'^competitions/$',  
        views.CompetitionList.as_view(), 
        name=views.CompetitionList.name), 
    url(r'^competitions/(?P<pk>[0-9]+)$',  
        views.CompetitionDetail.as_view(), 
        name=views.CompetitionDetail.name), 
    url(r'^$', 
        views.ApiRoot.as_view(), 
        name=views.ApiRoot.name), 
    ]

Now, we have to replace the code in the urls.py file in the restful01 folder, specifically, the restful01/urls.py file. The file defines the root URL configurations, and therefore, we must include the URL patterns declared in the previously coded drones/urls.py file. The following lines show the new code for the restful01/urls.py file. The code file for the sample is included in the hillar_django_restful_06_01 folder, in the restful01/urls.py file:

from django.conf.urls import url, include
 
urlpatterns = [
    url(r'^', include('drones.urls')),
]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset