So, now you have an idea of what an endpoint is, what do they look like? How do you create them in Django?
What are the endpoints we are going to use?
The endpoints we are going to use, consist of the following:
- GET /api/questions/
- POST /api/questions/
- GET /api/questions/1
- DELETE /api/questions/1
- GET /api/choices/1
- PUT /api/choices/1
Remember an endpoint can accept multiple HTTP Request Methods. I’ll be showing you how to allow an endpoint to accept different HTTP Request Methods as we go along.
How do you create an endpoint?
An endpoint is comprised of 2 very Django-y things.
You should know how to create both of those by now. However, in Django REST Framework, you’ll use different classes to automate the creation of them.
Let’s create our views!
Inside of our Polls app, I like to create a separate “views” file for API views. I just called mine “api_view.py“.
from rest_framework import generics from .models import Question, Choice from .serializers import QuestionSerializer, ChoiceSerializer class QuestionList(generics.ListCreateAPIView): # Get / Create questions queryset = Question.objects.all() serializer_class = QuestionSerializer class QuestionDetail(generics.RetrieveDestroyAPIView): # Get / Delete questions queryset = Question.objects.all() serializer_class = QuestionSerializer class ChoiceDetail(generics.RetrieveUpdateAPIView): # Get / Update a Choice queryset = Choice.objects.all() serializer_class = ChoiceSerializer
As you can see, each view has a specific name and subclasses a class from the rest_framework app that we installed from the first lesson.
Each view is being subclassed by different classes. ListCreateAPIView, RetrieveDestroyAPIView, RetrieveUpdateAPIView. The cool thing is, from their names you know exactly what they do!
ListCreateAPIView — lists the objects of a model AND allows the creation of a new object
RetrieveDestroyAPIView — Gets 1 object represented by the id of the object and allows the deletion of the object.
RetrieveUpdateAPIView — Gets 1 object represented by the id of the object and allows you to update the object.
Also, each view needs a
Once we have our views, which for this lesson is pretty easy, we now have to create the URLs so we can access each API view.
from django.conf.urls import url from . import views, api_views urlpatterns = [ # Regular Django Views # ... # API views url(r'^api/questions/$', api_views.QuestionList.as_view()), url(r'^api/questions/(?P<pk>[0-9]+)/$', api_views.QuestionDetail.as_view()), url(r'^api/choices/(?P<pk>[0-9]+)/$', api_views.ChoiceDetail.as_view()), ]
There is nothing special about the URLs. They are they same as any other Django application. You need to import api_views then, you access each view for each URL that you want to use as part of your API.
At this point, you can run
python manage.py runserver
If you did everything correctly, you should be able to go: http://localhost:8000/polls/api/questions/ and you’ll be able to play around with the API.
- Create the views and the URLs on your own app.
- If you are having problems look at my code or you can reply to this email and ask me any questions.
- Play around the with endpoints.
Next lesson, it is imperative that we update the Django views and templates so that it will use our new API to manipulate our data. We are going to use JQuery first, then as a Bonus lesson, we will use AngularJS.