I wanted to try the new Form.template_name
feature of Django 4.0 following this approach, but I am getting a TemplateDoesNotExist
error.
# forms.py
class MyForm(forms.Form):
title = forms.CharField()
template_name = "common/custom_form.html"
# In your template:
{{ form }}
# In templates/common/custom_form.html:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
It seems the renderer is not looking in the right directory. The debugger shows this:
Using engine django:
django.template.loaders.filesystem.Loader: /root/.cache/pypoetry/virtualenvs/project/lib/python3.9/site-packages/django/forms/templates/common/custom_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /root/.cache/pypoetry/virtualenvs/project/lib/python3.9/site-packages/django/contrib/admin/templates/common/custom_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /root/.cache/pypoetry/virtualenvs/project/lib/python3.9/site-packages/django/contrib/auth/templates/common/custom_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /root/.cache/pypoetry/virtualenvs/project/lib/python3.9/site-packages/allauth/templates/common/custom_form.html (Source does not exist)
django.template.loaders.app_directories.Loader: /root/.cache/pypoetry/virtualenvs/project/lib/python3.9/site-packages/ordered_model/templates/common/custom_form.html (Source does not exist)
Other templates used for views are loaded successfully from the the configured template directory.
# settings.py
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",
],
},
},
]
I’m using Django 4.0.1 running on Docker.
Am I missing anything? Is there any additional configuration needed for the form renderer? Many thanks!