#30978 closed Uncategorized (invalid)
Missing configuration step with custom filters.
Reported by: | blueglyph | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 2.2 |
Severity: | Normal | Keywords: | filters, configuration |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (5)
comment:1 by , 5 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Summary: | Missing configuration step with custom filters → Missing configuration step with custom filters. |
comment:2 by , 5 years ago
Resolution: | invalid |
---|---|
Status: | closed → new |
Hello,
Please don't close without investigating the problem first. I can reply to questions if something is unclear or requires more information.
When I remove the 'libraries' item, the site crashes as soon as I open the page corresponding to the template, with the following error:
TemplateSyntaxError at /profile/overview/3/ 'filters' is not a registered tag library. Must be one of: admin_list admin_modify admin_static admin_urls cache i18n l10n log static staticfiles
So it appears it is very necessary.
comment:3 by , 5 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Trac is not a support channel.
comment:4 by , 5 years ago
I'm reporting a problem in Django's documentation, I'm not looking for support since I worked out the missing information.
Now if you want to act like that and ignore the problem, that's entirely up to you. But nevertheless, you could remain courteous and not act like that to people who're trying to help.
comment:5 by , 5 years ago
This is not an issue in Django documentation but in your project. Custom filters for installed apps work without this step. You can try to follow an example from documentation and you will see that everything works.
But nevertheless, you could remain courteous and not act like that to people who're trying to help.
I don't see anything rude in my behavior, I tried to encourage you to use support channel because trac is not one of them.
You don't need to add custom template tag modules through the
libraries
argument toDjangoTemplates
when app is inINSTALLED_APPS
. This works for me.Please use one of support channels.