加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 在django中注册自定义过滤器

发布时间:2020-12-20 11:36:57 所属栏目:Python 来源:网络整理
导读:我的过滤器没有注册,也不确定它被绊倒的地方. 在test / templatetags中 __init__.pytest_tags.py test_tags.py包括 from django import templateregister.filter('intcomma',intcomma)def intcomma(value): return value + 1 test / templates包含pdf_test.h
我的过滤器没有注册,也不确定它被绊倒的地方.

在test / templatetags中

__init__.py
test_tags.py

test_tags.py包括

from django import template

register.filter('intcomma',intcomma)

def intcomma(value):
    return value + 1

test / templates包含pdf_test.html,其中包含以下内容

{% load test_tags %} 
<ul>
    <li>{{ value |intcomma |floatformat:"0"</li>
</ul>

浮动格式工作正常,但intcomma没有运气

解决方法

首先,你 haven’t defined register

To be a valid tag library,the module must contain a module-level
variable named register that is a template.Library instance,in which
all the tags and filters are registered.

另外,我通常用register.filter装饰这个功能:

from django import template

register = template.Library()

@register.filter
def intcomma(value):
    return value + 1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读