Boky
Boky

Reputation: 12064

Sending extracted database field to the django template

I have an view as follows :

def traxio_advice(request):
      calculation_list = Calculations.objects.select_related('customer').filter(user=request.user)
      p = Paginator(calculation_list, 20)

      page = request.GET.get('page')
      try:
           calculations = p.page(page)
      except PageNotAnInteger:
           calculations = p.page(1)
      except EmptyPage:
           calculations = p.page(p.num_pages)

      context = {'calculations': calculations}
      return render(request, 'master/archive.html', context)

Thus I get all calculations for this user. And the calculation model has an price field, but the price is exclusive VAT. It needs to be stored exclusive VAT in the database but I need to show it inclusive VAT.

Thus, is there any way to calculate the price for every calculation?

archive.html template is as follows :

{% for calculation in calculations %}
      <tr data-archive-row class="archive-row">
           <td>
               <span>{{ calculation.make }}</span><br>
               <small >{{ calculation.model }}</small>
           </td>
           <td>
               <span class="text-success text-semibold">
                   {{ calculation.purchase_price|floatformat:0|intcomma }}
               </span>
               <small class="text-muted">(incl. VAT)</small>
           </td>
      </tr>
{% endfor %}

Thus, in the template it's shown the price exclusive VAT.

I've created a function to convert it to the inclusive VAT :

def price_incl_vat(price_excl_vat):
    return (price_excl_vat * decimal.Decimal(settings.VAT_VALUE)).normalize()

Is there any way to calculate the price (call the function) in the view and then send it to the template or can it be done in the template?

Or is there any better way to do this?

Thanks in advance.

Upvotes: 1

Views: 75

Answers (2)

nimasmi
nimasmi

Reputation: 4138

  1. You ask

    or can it be done in the template?

    Yes, you can create a custom template filter. In yourapp/templatetags/yourapp_tags.py

    import decimal
    
    from django import template
    from django.conf import settings
    
    register = template.Library()
    
    
    # add VAT
    @register.filter(name='add_vat')
    def add_vat(price_excl_vat):
        return price_excl_vat * decimal.Decimal(settings.VAT_VALUE)).normalize()
    

    Then in the template

    {% load myapp_tags %}
    {% for calculation in calculations %}
        {{ calculation.purchase_price|add_vat }}
    {% endfor %}
    
  2. Alternatively, if you want this to be 'more' server-side (the template filter is still defined in python code I suppose) you could add a method on the model, so that it's available in more places.

    class Calculation(models.Model):
        # more stuff here
    
        @property
        def price_including_vat(self):
            return self.purchase_price * decimal.Decimal(settings.VAT_VALUE)).normalise()
    

    Then it's available anywhere, including in the template as calculation.price_including_vat.

Upvotes: 2

flowfree
flowfree

Reputation: 16462

You can put the price_incl_vat function as a method to your Model:

class Calculations(models.Model):
    # ... fields here ...

    def price_incl_vat(self):
        return (self.purchase_price * decimal.Decimal(settings.VAT_VALUE)).normalize()

Then you can call the method in your template:

<span>{{ calculation.price_incl_vat|intcomma }}</span>

Upvotes: 1

Related Questions