django-filter 仅在 venv 中查找模板

问题描述 投票:0回答:2

尝试为 RangeFilter 制作自定义小部件。每次 django 升值时

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)

django 搜索模板的每个路径都在 venv 中。当我将我的小部件与此路径匹配时,它工作得很好。

venv\lib\site-packages\django_filters emplates\MyRangeWidget.html

但我相信这不是正确的方法。

我的小部件:

<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 %}

过滤器.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']

小部件.py

import django_filters.widgets


class MyRangeWidget(django_filters.widgets.RangeWidget):
    template_name = 'MyRangeWidget.html'

设置.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',
            ],
        },
    },
]

顺便提一下,我在 template_name 中尝试了不同的路径。在我注意到 django 甚至没有尝试在除 venv 之外的任何地方搜索它之前,甚至包括绝对路径。

不知道是我的问题,还是django-filter包的问题。

django-templates django-filter django-settings django-widget
2个回答
1
投票

寻找如何修复它的方法的第二天,在我在这里询问后,我找到了方法。

在已安装的应用程序中添加了 django.forms 和 FORM_RENDERER 变量,如下所示:

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'

现在小部件中的 template_name 可以按预期工作。


0
投票

很好的答案!谢谢!我真的被卡住了

© www.soinside.com 2019 - 2024. All rights reserved.