Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling django template tags from a normal function

Tags:

python

django

How to call django template tags from a normal function?

For instance:

Django template tag:

@register.filter(name='CallingTemplateTagFunction')
def CallingTemplateTagFunction(price):    
    return price*10/100

I wanna to call GetValue function in a normal function.

For instance:

Normal python function:

def Test(request):
    CallingTemplateTagFunction(50) # How to call django template tag function?
like image 448
vamsi Avatar asked Nov 28 '25 01:11

vamsi


1 Answers

You can write somewhere your function:

def my_useful_function(price):
    return price * 10 / 100

And then use it in your views and in template tags:

from somewhere import my_useful_function

# in templatetags:    
@register.filter(name='calling_template_tag_function')
def calling_template_tag_function(price):    
    return my_useful_function(price)

And then in views:

def test(request):
   my_useful_function(50)
like image 83
amureki Avatar answered Nov 30 '25 16:11

amureki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!