[SOLVED] django-filter looking for templates only in venv

Issue

This Content is from Stack Overflow. Question asked by Danza

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:UsersAlexanderPycharmProjectsSearchvenvlibsite-packagesdjangoformstemplatesMyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:UsersAlexanderPycharmProjectsSearchvenvlibsite-packagesdjango_filterstemplatesMyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:UsersAlexanderPycharmProjectsSearchvenvlibsite-packagesdjangocontribadmintemplatesMyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:UsersAlexanderPycharmProjectsSearchvenvlibsite-packagesdjangocontribauthtemplatesMyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:UsersAlexanderPycharmProjectsSearchvenvlibsite-packagesdebug_toolbartemplatesMyRangeWidget.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:UsersAlexanderPycharmProjectsSearchvenvlibsite-packagesbootstrap5templatesMyRangeWidget.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.

venvlibsite-packagesdjango_filterstemplatesMyRangeWidget.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.



Solution

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.


This Question was asked in StackOverflow by Danza and Answered by Danza It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?