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 games/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.
The following lines show the new code that creates a JSONResponse
class and declares two functions: game_list
and game_detail
, in the games/views.py
file. 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. ...
Get Building RESTful Python Web Services 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.