【问题标题】:Django. TemplateDoesNotExist in case of a custom widget姜戈。 TemplateDoesNotExist 在自定义小部件的情况下
【发布时间】:2018-02-01 07:09:31
【问题描述】:

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

class FroalaWYSIWYGTextareaWidget(django.forms.widgets.Textarea):
    template_name = 'froala_wysiwyg.html'

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

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

这是我的设置:

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

通常一切正常,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

我调试了 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 路径来自哪里? 谢谢。

【问题讨论】:

    标签: python django django-forms django-templates


    【解决方案1】:

    如果您想使用存储在项目“TEMPLATES”目录下某处的自定义小部件模板,请按照以下步骤操作:

    a) 使用您在问题中提供的TEMPLATES 设置

    b) 在settings.py 中设置FORM_RENDERER 如下

    FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
    

    c) 将应用“django.forms”添加到settings.py 中的“INSTALLED_APPS”列表中

    另外,请务必将自定义小部件模板的正确路径分配给自定义小部件的“TEMPLATES”目录的“template_name”属性。

    【讨论】:

    • 恕我直言最佳答案
    • 很奇怪,它需要这个。
    • 反响很好。但是为什么在 django doc 中找不到呢?
    【解决方案2】:

    肯定不是bug

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

    你可以查看source code那里可以看到行

    [docs]class Textarea(Widget):
        template_name = 'django/forms/widgets/textarea.html'
    

    这是您第一个问题的来源。现在是第二个

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

    您的表单小部件类也是如此。为了使自定义小部件模板适合您,您必须使用相同的术语指定路径,例如 app_name/forms/widget/textarea.html

    【讨论】:

    • 好吧。但是,如果我只想为一个字段更改 textarea.html,我应该怎么做?
    • @Viach 在这种情况下,您可以继承此类并创建一个具有自定义 textarea.html 的新类,然后您可以在表单中的小部件参数中提供它
    • 我在与 djago 2.2 相同的上下文中尝试此操作无效!最后一个答案有效!
    猜你喜欢
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    相关资源
    最近更新 更多