Using decorators to enable different parsers and renderers
We will make changes to just one file. After you save the changes, Django's development server will automatically restart. However, you can decide to stop Django's development server and start it again after you finish all the necessary changes.
We will make the necessary changes to use the previously introduced @api_view
decorator to make it possible for the RESTful Web Service to work with different parsers and renderers, by taking advantage of generalized behaviors provided by the APIView
class.
Now, go to the restful01/toys
folder and open the views.py
file. Replace the code in this file with the following lines. However, take into account that many lines have been removed, such as the lines that declared the JSONResponse
class. The code file for the sample is included in the hillar_django_restful_04_02
folder, in the restful01/toys/views.py
file:
from django.shortcuts import render from rest_framework import status from toys...