Creating Django views combined with serializer classes
We have created the necessary model and its serializer. It is time to code the necessary elements to process HTTP requests and produce HTTP responses. Now, we will create Django views that use the ToySerializer
class that we created previously to return JSON representations of the entities for each HTTP request that our web service will handle. Open the toys/views.py
file. The following lines show the initial code for this file with just one import statement and a comment that indicates we should create the views:
from django.shortcuts import render # Create your views here.
We will create our first version of the web service and we will use functions to keep the code as simple as possible. We will work with classes and more complex code in later examples. First, it is very important to understand how Django and Django REST framework work by way of a simple example.
Now, write the following lines in the restful01/toys/views.py
file...