Form.template_name Issue: TemplateNotFound

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!

Something seems odd in that error message - it says it’s looking for “custom_form.html”, but the snippet is defined for “form_snippet.html”. Can you confirm that everything else on this view is working correctly by commenting out the template_name attribute on your MyForm class?

1 Like

Sorry, my bad. I’ve changed the filename to simplify the snippets, but didn’t update the error message… Changed it back to the filename I’m actually using. Hope it makes more sense now. Other that that everything else in the view is working, e.g. if I replace {{form}} with {{form.as_p}} or render the form fields individually within the view template.

You show your form as being in:

Is that in your BASE_DIR/templates directory or somewhere else?

yes exactly, there is a folder templates in the root directory of the project, where all templates are stored.

Try placing that form template in your app/templates/common directory, where app is the app containing that form.

1 Like

Mmm, interesting. If I put it into the templates folder with the app, i.e. my_app/templates/my_app/custom_form.html, it works. In a templates directory in the project root it doesn’t. Not quite sure why. But thanks for the suggestion.