Pass several attributes on Django

Asked

Viewed 53 times

0

My question is as follows. I am starting my studies in Jango and find a problem. I have a function in my view with the following excerpt

def saldo_total(request):
    receitas = Receitas.objects.aggregate(Sum('valor'))
    total_receitas = receitas['valor__sum']
    despesas = Despesas.objects.aggregate(Sum('valor'))
    total_despesas = despesas['valor__sum']

    receitas = receitas['valor__sum']
    despesas = despesas['valor__sum']

    total = total_receitas - total_despesas

    return render(request, 'financas/saldo_total.html', {'total': total}, {'receitas': receitas}, {'despesas': despesas})

it makes a search in db making the sum of revenues and expenses, and also below, subtracts one by the other to inform the user’s available balance.

However, the surrender only allows me to play an attribute to the return and I would like the three to be returned.

Is there any way to do it on Django?

1 answer

0

Yes. What do you put in render is a dictionary, and dictionaries may contain more than one key/value pair.

What you want is equivalent to the following:

return render(request, 'financas/saldo_total.html', {'total': total, 'receitas': receitas, 'despesas': despesas})

Or in a more organized way:

dados = {'total': total, 
         'receitas': receitas,
         'despesas': despesas }
return render(request, 'financas/saldo_total.html', dados)
  • Thanks, buddy. It worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.