gpt4 book ai didi

python - 从普通函数调用 django 模板标签

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:05 25 4
gpt4 key购买 nike

如何从普通函数中调用django模板标签?

例如:

Django 模板标签:

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

我想在普通函数中调用 GetValue 函数。

例如:

普通python函数:

def Test(request):
CallingTemplateTagFunction(50) # How to call django template tag function?

最佳答案

你可以在某处写下你的函数:

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

然后在您的 View 和模板标签中使用它:

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)

然后在 View 中:

def test(request):
my_useful_function(50)

关于python - 从普通函数调用 django 模板标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com