Now we will create Django views that will use the previously created GameSerializer
class to return JSON representations for each HTTP request that our API will handle. Open the views.py
file located within the games_service/games
folder. 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.
Replace the existing code with the following lines. The new code creates a JSONResponse
class and declares two functions: game_collection
and game_detail
. We are creating our first version of the API, and we use functions to keep the code as simple as possible. We will work with classes and more complex code in the next examples. The highlighted lines show the expressions that evaluate the value of the request.method
attribute to determine the actions to be performed based on the HTTP verb. The code file for the sample is included...