stephan
stephan

Reputation: 2393

What's wrong with my template filter?

I tried to write a simple template filter to round off integers to the nearest half. If you see my code below you can follow what I'm trying to do.

@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

The problem is when I use this inside a template, I get invalidfilter: roundnumber

{{ staravg.stars__avg|roundnumber }}

Upvotes: 0

Views: 46

Answers (1)

falsetru
falsetru

Reputation: 368964

To use the customer template tags or filters, you need to load them in the template.

{% load customer_templatetags %}

NOTE: Make sure the app that contains custom tags/filters are listed in INSTALLED_APPS. Also make sure the templatetags directory have an __init__.py file.

Upvotes: 1

Related Questions