I'm trying to make a webpage in which you can specify a number of vectors, then insert modulus and angle for each one, and it will calculate the sum. My code can do the calculation, but I can't properly display the result on my template.
views.py:
def sum_view(request):
if request.method == "POST":
message = ''
for j in range(2,12):
if not request.POST.get(f'vector_mod_{j}'):
num_vett = j - 1
result = Vector(0, 0)
for i in range(num_vett):
mod = float(request.POST.get(f'vector_mod_{i+1}'))
ang = float(request.POST.get(f'vector_ang_{i+1}'))
result += Vector(mod, ang)
message = f'Modulus: {result.modulus}, angle: {result.angle}°'
return JsonResponse({'message': message})
return render(request, 'vectsum/sum.html')
The problem is that when I submit I see a sort of firefox developer tools view with a menu including 'JSON', 'Raw Data', 'Headers', and the correct message displayed in a console-like way.
Here is the ajax part of my template:
$(document).on('submit','#calculate',function(e){
e.preventDefault();
$.ajax({
type:'POST',
headers: {'X-Requested-With': 'XMLHttpRequest'},
url:'/vectorialsum/',
data: $('#calculate').serialize()
success:function(data){
var mess = JSON.parse(data)['message']
document.getElementById('result').innerHTML += mess
}
})
});
Probably I don't get what return JsonResponse does, however how do I show the message in my page?
Edit:
maybe my question is not clear, I'll rephrase: how can I send back to ajax's success function the message
variable?
result
, try to apply to c exact instance in stack -result[0].modulus
, orresult[0]
result
should be: list, dict, tuple or set? And what isVector
here?