from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from math import sqrt
from fractions import Fraction
def index(request):
return render(request, 'home/index.html', {})
def multiplication(request):
return render(request, 'multiplication/multiplication.html', {})
def compute(request):
a = int(request.POST.get("a"))
b = int(request.POST.get("b"))
c = int(request.POST.get("c"))
det = b*b-4*a*c
rdet = sqrt(det)
x1= (-b-rdet)/(2*a)
x2= (-b+rdet)/(2*a)
x3= Fraction(15, 45)
return JsonResponse({"op_result": det, "op_result2": x1, "op_result3": x2, "op_result4": 2*a, "op_result5": -b, "op_result6": c,"op_result7": x3})
in my view.py, I tried to send back to my html, the results of the function fractions through the var x3
x3= Fraction(15, 45) and sent the result with JsonResponse. But I have an error message :
object of type fraction is not json serializable
I do not understand where is the mistake.
Thank you for your help
you cannot serialize a fraction but can make a custom serializer and pass numerator and denominator in that
this is output in jsonresponse
[![enter image description here][1]][1]
rather than sending as fraction just compute it and send it as string would be a easier approach