Why is this template filter invalid?

放肆的年华 提交于 2019-12-25 04:09:33

问题


What am I doing wrong here?

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?


回答1:


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
    # ...


来源:https://stackoverflow.com/questions/27613563/why-is-this-template-filter-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!