【问题标题】:Django 1.11 custom widget template TemplateDoesNotExist [duplicate]Django 1.11 自定义小部件模板 TemplateDoesNotExist [重复]
【发布时间】:2017-07-05 18:27:46
【问题描述】:

我正在尝试用我自己的替换小部件模板,但我收到了 TemplateDoesNotExist 错误。

在 app\forms.py 中:

class SelectPlus(Select):
    template_name = 'selectplus.html'


class SimpleForm(ModelForm):
    somefield = ModelChoiceField(
        queryset=SomeObjects.objects.all(), 
        widget=SelectPlus(attrs={'url': 'custom_url_to_context'})
    )

在settings.py中

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [ROOT_PATH, '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',
        ],

    },
},

在调试跟踪中,我看到模板加载器没有在应用程序文件夹或主模板文件夹中搜索:

但所有其他模板都可以正常工作。

【问题讨论】:

  • 模板文件夹是在应用还是在项目级别?
  • 模板文件夹在项目根文件夹中
  • 您为我指明了正确的方向!我在 app 文件夹中创建了模板文件夹并找到了模板!
  • 很好,这是一种方式,另一种方式是修复目录中的模板路径,你错了。

标签: python django


【解决方案1】:

在 Django 1.11 中:

# app/widgets.py
from django.forms.widgets import DefaultWidget

class SelectPlus(DefaultWidget):
    template_name = 'app/selectplus.html'

导入您要修改的小部件。您可以在这里找到您的选择:https://docs.djangoproject.com/en/1.11/ref/forms/widgets/#built-in-widgets

创建一个子类并指定模板名称。

# app/templates/app/selectplus.html
<select name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %}
  <optgroup label="{{ group_name }}">{% endif %}{% for option in group_choices %}
  {% include option.template_name with widget=option %}{% endfor %}{% if group_name %}
  </optgroup>{% endif %}{% endfor %}
</select>

创建您的模板。这是从 django/forms/widgets/select.html 完全复制的,但你可以在这里放任何你想要的东西。您还可以参考其他现有的小部件模板以获取灵感。您可以在这里找到您的选择:https://github.com/django/django/tree/master/django/forms/templates/django/forms/widgets

# app/forms.py
from .widgets import SelectPlus

class SimpleForm(ModelForm):
    my_field = forms.ExampleField(widget=SelectPlus())

导入您的客户小部件 (SelectPlus) 并将其添加到表单中的字段中。

最后,在您的模板中,渲染主字段,它将引用 app/templates/app/selectplus.html。

【讨论】:

    【解决方案2】:

    您有两种方法可以解决此问题。

    1) 修复你的模板路径,像这样。

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ROOT_PATH, os.path.join(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',
            ],
    
        },
    }
    

    2) 在应用内添加模板文件夹。

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 1970-01-01
      • 2020-05-22
      • 2018-02-01
      • 2013-04-06
      • 1970-01-01
      • 2012-05-26
      • 2013-04-03
      • 2019-02-27
      相关资源
      最近更新 更多