为什么这个模板过滤器无效? [英] Why is this template filter invalid?

查看:361
本文介绍了为什么这个模板过滤器无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里做错什么?

我在位于templatetags目录中的名为accounts_extras.py的模块中有圆括号。在我的模板中,我的顶部有{%load accounts_extras%}。还有一点值得注意的是,upto目前正在另一个模板中工作(尚未在此模板上尝试过),但问题在于循环数。 {{staravg.stars__avg | roundnumber}}给我一个无效的过滤器错误。

I have roundnumber in a module called accounts_extras.py located in the templatetags directory. In my template I have {% load accounts_extras %} at the top. It's also worth noting that 'upto' is currently working in another template (haven't tried it on this template yet), but the issue is with roundnumber. {{ staravg.stars__avg|roundnumber }} is giving me an invalid filter error.

#accounts_extras.py
from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
@stringfilter
def upto(value, delimiter=None):
    return value.split(delimiter)[0]
upto.is_safe = True


@register.filter
def roundnumber(value):
    if value > 1.75 and value < 2.25
    return 2
    if value > 2.25 and value < 2.75
    return 2.5
    if value > 2.75 and value < 3.25
    return 3
    if value > 3.25 and value < 3.75
    return 3.5
    if value > 3.75 and value < 4.25
    return 4

问题是我在同一个模块中有两个过滤器?这是否允许?

Is the problem that I have two filters in the same module? Is that allowed?

推荐答案

您的过滤器定义很好。问题是丢失的冒号和缩进:

Your filter definition is fine. The problem is with the missing colons and indentation:

@register.filter
def roundnumber(value):
    if value > 1.75 and value < 2.25: # colon
        return 2 # indentation
    # ...

这篇关于为什么这个模板过滤器无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆