“'tag' 不是已注册的标签库。必须是 Django 应用程序中的其中之一”

Mad*_*ham 3 tags django

我在模板标签中将我的简单标签添加到我的文件中。我的第一个标签是可见的并且可以正常工作,但第二个标签不起作用。我''deposit_earn' is not a registered tag library. Must be one of:'在模板中添加标签后收到信息{% load deposit_earn %}

我的标记文件如下所示:

@register.simple_tag()
def multiply(number_instalment_loans, rrso, balance):
    rrso_percent = rrso/100
    return round(discounted_interest_rate(12, number_instalment_loans, rrso_percent, balance))

@register.simple_tag()
def deposit_earn(period, interest, balance):
    interest_percent = interest/100
    equals = balance * interest_percent * period / 12
    return round(equals)
Run Code Online (Sandbox Code Playgroud)

为什么我的第一个标签有效,而第二个无效?我在注册标签后尝试重置服务器,但没有帮助。

小智 8

https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/

你应该导入模板标签文件名,而不是方法名

polls/
    __init__.py
    models.py
    templatetags/
        __init__.py
        poll_extras.py
    views.py
Run Code Online (Sandbox Code Playgroud)

假设在 poll_extras.py 中进行乘法和存款赚取

然后在你的模板中,你只需要调用 poll_extras

{% load poll_extras %}

{{ something|multiply }}
or
{{ something|deposit_earn }}
Run Code Online (Sandbox Code Playgroud)