Writing API views

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

Get Hands-On RESTful Python Web Services - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.