0

I have defined the following code in the views.py I am getting TypeError : Object of type function is not JSON serializable

Libaries:

from django.db.models.query import QuerySet
from django.shortcuts import render
from django.http import HttpResponse , Http404 , JsonResponse
import json 
from json import JSONEncoder
from FeedPost.models import trill

def home(request, *args, **kwargs):
    return render(request, "feed.html", context={}, status=200)

Where the error occured

def trills_list_view(request, *args , **kwargs):
    qs = trill.objects.all()
    trills_list = [{"id": x.id, "name": x.name} for x in qs]
    data = {
             "response": trill_list
           }
    return JsonResponse(data)


def trill_list(request, trill_id, *args, **kwargs):
  
    data = {
             "id" : trill_id,
           }
    status = 200
    try:
        obj = trill.objects.get(id=trill_id)
        data['name'] =obj.name
    except:
        data['message'] = "Not found"
        status = 404
     
    return JsonResponse(data, status=status)
    
Prakhar
  • 929
  • 1
  • 9
  • 19
  • Hi! Can you improve the formatting and indentation please? And also if possible share the whole traceback so it would be easy to find the exact line causing the problem – Brian Destura Jul 13 '21 at 07:21
  • https://www.youtube.com/watch?v=f1R_bykXHGE This is the tutorial i was following and you can refer the same .... YOu can skip to 01:12 hrs to know exactly what is going on – Sreeni Vas Jul 13 '21 at 07:51
  • **Typo**: `trill_list` is the name of your function whereas `trills_list` (notice the `s`) is the name of your variable, hence when you write `"response": trill_list` you get the error, you want to be writing `"response": trills_list` instead. – Abdul Aziz Barkat Jul 13 '21 at 08:34
  • [Possible duplicate or related question](https://stackoverflow.com/q/3768895/4575793) – Cadoiz Sep 08 '21 at 13:24

0 Answers0