Tried to make custom widget for RangeFilter. Every time django raises
TemplateDoesNotExist
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django\forms\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django_filters\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django\contrib\admin\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\django\contrib\auth\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\debug_toolbar\templates\MyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Alexander\PycharmProjects\Search\venv\lib\site-packages\bootstrap5\templates\MyRangeWidget.html (Source does not exist)
Every path django search for template is in venv. And when i put my widget to match this path, it working just fine.
venv\lib\site-packages\django_filters\templates\MyRangeWidget.html
But i believe its not correct way to do this.
My widget:
<div class="input-group">
{% for widget in widget.subwidgets %}
{% if forloop.first %}
<span class="input-group-text"><i class="bi bi-chevron-left"></i></span>
{% endif %}
{% if forloop.last %}
<span class="input-group-text"><i class="bi bi-chevron-right"></i></span>
{% endif %}
{% include widget.template_name %}
{% endfor %}
filters.py
import django_filters
from .models import *
from .widgets import MyRangeWidget
class LandFilter(django_filters.FilterSet):
price = django_filters.RangeFilter(widget=MyRangeWidget)
size = django_filters.RangeFilter(widget=MyRangeWidget)
class Meta:
model = PriceChangeHistory
fields = ['price', 'size']
widgets.py
import django_filters.widgets
class MyRangeWidget(django_filters.widgets.RangeWidget):
template_name = 'MyRangeWidget.html'
settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = [
'django_filters',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'bootstrap5',
'search',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
Just to mention, i tried different paths in template_name. Including even absolute path before i notice that django not even try to search for it anywhere except venv.
Have no idea if its me, or its django-filter package problem.
2nd day of searching way how to fix it, and right after i ask it here i found the way.
Added django.forms in installed apps and FORM_RENDERER variable like so:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.forms',
'django_filters',
'debug_toolbar',
'bootstrap5',
'search',
]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
Now template_name in widget works as expected.