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)
    
4
  • 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 Commented Jul 13, 2021 at 7:21
  • 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 Commented Jul 13, 2021 at 7: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. Commented Jul 13, 2021 at 8:34
  • Possible duplicate or related question Commented Sep 8, 2021 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.