Django JSON Data

Implement JsonResponse in Django API for JSON data

Poulami Dutta

--

Django counts with the built-in JsonResponse class, which is a subclass of HttpResponse. Its default Content-Type header is set to application/JSON, which is really convenient. It also comes with a JSON encoder, so you don’t need to serialize the data before returning the response object.

Here is the short version of how to implement it.

  1. In views.py , make API that would call the JsonResponse for fetching the respective data.

2. Inside the product, in urls.py, call the functions in order to fetch the data in JSON format.

3. In onlinestore, urls.py has to redirect the call to the product routes.

Now, these are the final steps for checking the results, after these steps:

  • localhost:8000/api/products(this will display all the products in json)
  • localhost:8000/api/products/1(this will display data of id=1 in json)

If you want to visit a completely functional code, do visit my Github repo: https://github.com/Poulami96/JsonResponse-Django-

Do give it a try .Trust me its pretty simple and do google if you are stuck anywhere.

--

--

Poulami Dutta

Software Developer Engineer (Python Developer, Ex-ISRO)