0

My django project it's about money management, so one of it's type of money transaction is MultipleTransaction, it only says that it was made by multiple users together.

I want to show in my interface the total value from one MultipleTransaction, using the Django template engine, how do I do that?

I tried the following:

template.html

{% with total=transaction.value*transaction.users.count %}
   R${{ total }}
{% endwith %}

But I get this error message:

TemplateSyntaxError at /historico/

Could not parse the remainder: '*transaction.users.count' from 'transaction.value*transaction.users.count'

Model.py

# An abstract class containing info about a Transaction made by multiple users
class MultipleTransaction (Info):
    # All users that paid the the monthly bill
    users = models.ManyToManyField(User)

    #A file as receipt, it can be an image or a pdf. This is optional
    receipt = models.FileField(
        'Comprovante(s)',
        upload_to='comprovantes',
        blank=True,
        null=True
    )

    class Meta:
        # Make this class an Abstract class
        abstract = True
        # Human friendly singular and plural name
        verbose_name = 'Transação em Grupo'
        verbose_name_plural = 'Transações em Grupo'

View.py

@login_required
def historico(request):
    # Those 3 lines are queries of all transactions
    deposits = chain(Deposit.objects.all(), MonthlyDeposit.objects.all())
    withdraws = chain(Withdraw.objects.all(), EventSubscription.objects.all())
    transferences = Transference.objects.all()

    # Then all transactions are sorted by their atribute "created_at"
    transactions = sorted(
        chain(deposits,withdraws,transferences),
        key=lambda instance: instance.created_at
    )

    # Get all boxes to show their values
    boxes = Box.objects.all()

    return render(
        request,
        'shell/app_shell.html',
        {
            'is_varys': True,
            'transactions': transactions,
            'boxes':boxes,
            'title': 'Histórico'
        }
    )
Kanagawa Marcos
  • 100
  • 1
  • 9

3 Answers3

3

The Django template language does not support multiplication. You could add a method to your model.

class MultipleTransaction (Info):
    # All users that paid the the monthly bill
    users = models.ManyToManyField(User)

    def calc_total_value(self):
        return self.value * self.users.count()


R${{ transaction.calc_total_value }}
Alasdair
  • 298,606
  • 55
  • 578
  • 516
1

You can't simply multiply variables in templates. You must either write your own custom template tag for multiplication or simply do calculations on the model side. Adding a custom method in your model to calculate total transaction value is a good way to go.

0

Try with

transaction.users.all().count

Or

transaction.all()[0].users.count
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • **error 1:** Could not parse the remainder: '*transaction.users.all().count' from 'transaction.value\*transaction.users.all().count' **error 2:** Could not parse the remainder: '*transaction.all()[0].users.count' from 'transaction.value\*transaction.all()[0].users.count' – Kanagawa Marcos Apr 17 '18 at 18:29