frequent
frequent

Reputation: 28513

How to add a custom tag/filter to an existing Django app?

I have to adapt a Django application (first time working with Django) and I'm running into issues getting a custom tag/filter to work. The app I'm working with has the following structure:

tool
|- manage.py
|- templates
|- ...
|- vote
    |- initadmin
    |- initproc
        |- templatetags
            | __init__.py
            | guard.py
        |- __init__.py
        |- apps.py
        |- models.py
        |- views.py
    |- __init__.py
    |- urls.py
    |- settings.py
    |- __pycache__
    |- wsgi.py

In settings.py my installed apps contains:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',

    # 3rd party
    ...

    # must be before admin ...
    'django.contrib.admin',

    # locally
    'vote.initadmin',
    'vote.initproc',
]

I wanted to add a custom tag, which I could load from within templates {% load site_defaults %} to add certain default parameters to templates. From the docs I understand, that I would have to:

1) add a module to templatetags, so I created

# /vote/initproc/templatetags/site_defaults.py
import os
from django.conf import settings
from django import template
from six.moves import configparser

site_parser = configparser.ConfigParser()
site_parser.read(os.path.join(settings.BASE_DIR, "init.ini"))

register = template.Library()

@register.filter(name='site_defaults')
def get_setting(my_setting):
  return site_parser.get("settings", my_setting)

2) Load this in a template and call my method, for example:

{% load static %}
{% load i18n %}
{% load site_defaults %}
<!DOCTYPE html>
<html lang="{% get_setting DEFAULT_LANGUAGE %}">
...

3) Add the module to the INSTALLED_APPS in settings.py

but as the list already includes an entry for vote.initproc and the contained templatetags (like guard) are assumingly loading I thought this was not required.

Restarting the server works, but when I load a page, I get TemplateSyntaxError: 'get_setting'. Did you forget to register or load this tag?.

I tried adding my site_defaults to both the installed_apps and middleware, but it throws ModuleNotFoundError: No module named 'site_defaults'

The Django docs only mention:

The app that contains the custom tags must be in INSTALLED_APPS in order for the {% load %} tag to work.

but I'm lost on how to add it correctly, so question is: How do I correctly add a custom tag/filter to an existing Django app?

Upvotes: 0

Views: 1235

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

You are registering a filter but trying to use it as a tag, and you are registering it under the name "site_defaults" and try to use it as "get_settings". You want simple_tag here, and leave the name alone:

@register.simple_tag
def get_setting(my_setting):
   # ...

https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/#simple-tags

Upvotes: 3

Related Questions