Django的.在自定义小部件的情况下TemplateDoesNotExist

MrB*_*Win 16 python django django-templates django-forms

我正在尝试在Django管理员中创建自定义小部件.我创建了一个类:

class FroalaWYSIWYGTextareaWidget(django.forms.widgets.Textarea):
    template_name = 'froala_wysiwyg.html'
Run Code Online (Sandbox Code Playgroud)

然后一个简单的模型形式:

class ArticleForm(django.forms.ModelForm):
    class Meta:
        fields = '__all__'
        model = Article
        widgets = {
            'content': FroalaWYSIWYGTextareaWidget(),
        }
Run Code Online (Sandbox Code Playgroud)

这是我的设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_PATH, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.i18n',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

Usualy一切正常,Django可以在我的/ templates /目录中找到模板,但是在这个小部件的情况下,我有一个500错误:

TemplateDoesNotExist at /admin/article/article/1/change/
froala_wysiwyg.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/article/article/1/change/
Django Version: 1.11.4
Exception Type: TemplateDoesNotExist
Exception Value: froala_wysiwyg.html
Exception Location: /home/username/.virtualenvs/sitename/lib/python3.5/site-packages/django/template/engine.py in find_template, line 148
Python Executable:  /home/username/.virtualenvs/sitename/bin/python
Python Version: 3.5.2
Run Code Online (Sandbox Code Playgroud)

我调试了django.filesystem.loader并发现通常Loader.engine.dirs是一个列表: ['/home/username/python/sitename/templates'] 所以Loader.get_template_sources()工作得很好

但是在这个自定义小部件的情况下,这个loader.engine.dirs仅包含: ['/home/username/.virtualenvs/sitename/lib/python3.5/site-packages/django/forms/templates']

所以它只是忽略DIRS了设置中的选项,而是使用表单/模板.这是Django的错误还是我必须在设置中更改某些内容?我不明白这条django/forms/templates道路来自哪里?谢谢.

use*_*125 15

如果要使用存储在项目的"TEMPLATES"目录下某处的自定义窗口小部件模板,请按照下列步骤操作:

  1. 使用您在问题中提供的TEMPLATES设置(在下面重复):

    TEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS':[os.path.join(BASE_PATH, '模板')], 'APP_DIRS':真, '选项':{ 'context_processors':[ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.template.context_processors.media', 'django.template.context_processors.static',"django.template. context_processors.i18n", 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages',],},},]

  2. 在settings.py中设置FORM_RENDERER,如下所示

    FORM_RENDERER ='django.forms.renderers.TemplatesSetting'

  3. 将应用程序"django.forms"添加到settings.py中的"INSTALLED_APPS"列表中

此外,请确保将自定义窗口小部件模板相对于"TEMPLATES"目录的正确路径分配给自定义窗口小部件的"template_name"属性.

  • 恕我直言最佳答案 (2认同)
  • 太奇怪了,它需要这个。 (2认同)
  • 反应很好。但为什么在 django doc 中找不到它? (2认同)

Arp*_*nki 9

它当然不是一个bug

我不明白这个django/forms/templates路径来自哪里?

您可以查看可以看到该行的源代码

[docs]class Textarea(Widget):
    template_name = 'django/forms/widgets/textarea.html'
Run Code Online (Sandbox Code Playgroud)

这是你的第一个问题的来源.现在是第二个

此渲染器使用独立的DjangoTemplates引擎(未连接到您在TEMPLATES设置中配置的内容).它首先从django/forms/templates中的内置表单模板目录加载模板,然后使用app_directories加载器从已安装的应用程序模板目录中加载模板.

对于表单窗口小部件类也是如此.要使自定义窗口小部件模板正常工作,您必须使用相同的术语指定路径app_name/forms/widget/textarea.html

  • 我在 djago 2.2 的相同上下文中尝试这样做,但不起作用!最后的答案有效! (2认同)