Lesson 2 -- Endpoints

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:

  1. GET /api/questions/
  2. POST /api/questions/
  3. GET /api/questions/1
  4. DELETE /api/questions/1
  5. GET /api/choices/1
  6. 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.

  1. View
  2. URL

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”.

api_views.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 queryset and serializer_class attributes.

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.

polls/urls.py

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.

Homework

  1. Create the views and the URLs on your own app.
  2. If you are having problems look at my code or you can reply to this email and ask me any questions.
  3. 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.

I love AngularJS as a Javascript framework, but it can get messy with Django because of the special Django template tags. They tend to cause a problem. But, we’ll look into how we can still use AngularJS in our Django project. There will also be another bonus lesson too! So, stay tuned!

Chris