#30978: Missing configuration step with custom filters
-------------------------------------+-------------------------------------
               Reporter:  blueglyph  |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:             |        Version:  2.2
  Documentation                      |       Keywords:  filters,
               Severity:  Normal     |  configuration
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Target documentation page: [https://docs.djangoproject.com/en/2.2/howto
 /custom-template-tags/]

 It seems the TEMPLATES settings must include a new element in order to
 properly register custom filters:
 {{{
  ['OPTIONS']['libraries']
 }}}


 My structure is:


 {{{
 profile/
 - __init__.py
 - models.py
 - urls.py
 - views.py
 - templetags/
 - - __init__.py
 - - filters.py

 templates/
 - profile/
 - - overview.html

 }}}

 Excerpt of filters.py:

 {{{
 from profile.models import Profile
 from django import template

 register = template.Library()

 @register.filter(name='she_he')
 def she_he(profile: Profile, cap=False):
         pronoun = 'she' if profile.gender == 'F' else 'he'
         return pronoun.capitalize() if cap else pronoun
 }}}

 Excerpt of overview.html:

 {{{
 {% extends "base.html" %}
 {% load filters %}
 {% block content %}
 <p>{{ profile|her_his }} current role
 }}}

 My app is declared in INSTALLED_APPS, and it worked before I added the
 filter and the load in the template.
 Yet I got this error:


 {{{
 'filters' is not a registered tag library. Must be one of:
 [...]
 }}}



 To make it work, I had to add the 'libraries' item in TEMPLATES, in
 settings.py:


 {{{
 TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
         'DIRS': [os.path.join(BASE_DIR, 'templates')]
         ,
         'APP_DIRS': True,
         'OPTIONS': {
             'context_processors': [
                 'django.template.context_processors.debug',
                 'django.template.context_processors.request',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
             ],
             'libraries': {
                 'filters': 'profile.templetags.filters'
             }
         },
     },
 ]

 }}}

 I don't see that on that page, which makes it very confusing.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30978>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.96fe1046cba243d19288cc3f7f400956%40djangoproject.com.

Reply via email to