1

im new in django and python . in my template i need to pass two value to a template tag . this two value come from view.py .i read Django template, send two arguments to template tag? but i can resolve my problem.

view.py:

def order(request):
   .
   .
   . 
 return render(request, 'cart.html', {
                "total_price": total_price,
                "final_verified": profile.teacher.final_verified,
                "cart": the_cart,
                "cart_items": the_cart.cartitem_set.filter(deleted=False),
                "discount": discount
            })

template.html:

{% load tags %}

 <div class="pull-left">
                      <i>
                        {% if final_verified %}
                          {{ total_price|rials_to_toman|intcomma:False }}
                          {%  final_price total_price discount %} <!--problem is here-->
                        {% else %}
                          0
                        {% endif %}
                       </i>Price
 </div>

tag.py:

from django import template
register = template.Library()

@register.simple_tag
def final_price(num,discount):   
    return str((int(num) * int(discount)) // 100)

@register.filter
def rich_to_pay_calc(num):
    if num:
        return str((int(num)*8 // 10) + 3000)
    else:
        return ""

@register.filter
def percent_80(num):
    if num:
        return str((int(num) * 8) // 10)
    else:
        return ""


@register.filter
def truncate(num):
    num = str(num)
    return num[:3]

first i used {%load final_price%} at top of template but this Error occur when i try to load url of this template :

TemplateSyntaxError at /order/cart/
'final_price' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_static
admin_urls
cache
humanize
i18n
l10n
log
static
staticfiles
tags
tz

in tags.py some other tag exist (as above code u can see) . its funny that other tag will accessible in template except final_price that is "simple_tag" instead "filter" . im new in django and web developing so forgive if problem is silly . tnx

mehdi
  • 1,100
  • 1
  • 11
  • 27

2 Answers2

0

As you have used simple tag, I think you need to load the tag name itself as:

 {% load final_price %} <--add

 <div class="pull-left">
         <i>
         {% if final_verified %}
             {{ total_price|rials_to_toman|intcomma:False }}
             {% final_price total_price discount %}
                    {% else %}
                      0
                    {% endif %}
                   </i>Price
</div>

Hope it works

Sanip
  • 1,772
  • 1
  • 14
  • 29
0

Use {% load tag %} in template.html instead of {% load tags %}

atiquratik
  • 1,296
  • 3
  • 27
  • 34
Vitalii Mytenko
  • 544
  • 5
  • 20