stephan
stephan

Reputation: 2393

Why is this template filter invalid?

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?

Upvotes: 0

Views: 286

Answers (1)

nima
nima

Reputation: 6733

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

Upvotes: 1

Related Questions