[Django] #36044: Reusable form templates example lead to a TemplateDoesNotExist error

38 views
Skip to first unread message

Django

unread,
Dec 29, 2024, 1:45:58 PM12/29/24
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Type:
| Uncategorized
Status: new | Component:
| Documentation
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Following the docs https://docs.djangoproject.com/en/5.1/topics/forms
/#reusable-form-templates, i tried to set a a form template.

settings.py
{{{
#...
#...

from django.forms.renderers import TemplatesSetting


class CustomFormRenderer(TemplatesSetting):
form_template_name = "form_snippet.html"


FORM_RENDERER = "django_bug.settings.CustomFormRenderer"

}}}

models.py

{{{
from django.db import models

class Car(models.Model):
name = models.CharField(max_length=100)

}}}

forms.py
{{{

from cars.models import Car
from django import forms


class CarForm(forms.ModelForm):
class Meta:
model = Car
fields = '__all__'

}}}

views.py

{{{
from django.views.generic import CreateView

from cars.forms import CarForm
from cars.models import Car


class CarCreateView(CreateView):
model = Car
form_class = CarForm

}}}

template/cars/car_form.html

{{{
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

This is form:

{{ form }}

</body>
</html>
}}}

template/form_snippet.html
{{{

{% for field in form %}
<div class="fieldWrapper">
wrapper from snippet
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}

}}}

and then:
{{{
TemplateDoesNotExist at /create_car/

django/forms/errors/list/default.html

Request Method: GET
Request URL: http://localhost:8000/create_car/
Django Version: 5.1.4
Exception Type: TemplateDoesNotExist
Exception Value:

django/forms/errors/list/default.html
}}}

So either i missed a step but maybe the doc example is not very clear, or
the example should be modified because inheriting from `DjangoTemplates`
instead of `TemplateSettings` made the example work out of the box:

{{{
from django.forms.renderers import DjangoTemplates


class CustomFormRenderer(DjangoTemplates):
form_template_name = "form_snippet.html"


FORM_RENDERER = "django_bug.settings.CustomFormRenderer"

}}}

I tried the search engine issue but didn't find any related ones. Can i
propose an update to the docs ?
--
Ticket URL: <https://code.djangoproject.com/ticket/36044>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 30, 2024, 2:14:26 AM12/30/24
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
------------------------------------+--------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Uncategorized | Status: new
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Comment (by Asfand Yar Khan):

Yes it does work with DjangoTemplates , I am not sure but can someone
confirm if this is just a doc change ?
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:1>

Django

unread,
Dec 30, 2024, 4:24:39 PM12/30/24
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
------------------------------------+--------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Uncategorized | Status: new
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Comment (by Tim Graham):

I'm inclined to think you probably didn't configure `TemplatesSetting`
[https://docs.djangoproject.com/en/dev/ref/forms/renderers/#templatessetting
as documented]. i.e. "Using this renderer requires you to make sure the
form templates your project needs can be located."

I'm unsure if modifying the example would improve it since what works "out
of the box" for one project may not work for another.
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:2>

Django

unread,
Dec 31, 2024, 10:52:24 PM12/31/24
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
------------------------------------+--------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Uncategorized | Status: new
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Comment (by Asfand Yar Khan):

Thanks, works after configuring TemplatesSetting.
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:3>

Django

unread,
Dec 31, 2024, 11:01:16 PM12/31/24
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
------------------------------------+--------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Uncategorized | Status: closed
Component: Documentation | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Asfand Yar Khan):

* resolution: => invalid
* status: new => closed

--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:4>

Django

unread,
Jan 1, 2025, 4:12:09 AM1/1/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
------------------------------------+--------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Uncategorized | Status: new
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Guillaume LEBRETON):

* resolution: invalid =>
* status: closed => new

Comment:

You're right, i didn't configure `TemplateSetting`, because in the doc
paragraph https://docs.djangoproject.com/en/5.1/topics/forms/#reusable-
form-templates, there's no mention of any prior conffiguration needed. It
says you have to configure a form renderer, and in the link there's 3
different form renderers:
https://docs.djangoproject.com/en/5.1/ref/settings/#form-renderer, but
somehow the one that doesn't need configuration is not used in the
example.

Let me summarize how a user would use the doc, here is the actual
situation:
- You create a new django project, without any customization
- You create a form view
- You copy paste snippets from
https://docs.djangoproject.com/en/5.1/topics/forms/#reusable-form-
templates
- You have an error about missing templates
- You click on https://docs.djangoproject.com/en/5.1/ref/settings/#std-
setting-FORM_RENDERER
- You click on
https://docs.djangoproject.com/en/5.1/ref/forms/renderers/#django.forms.renderers.TemplatesSetting
- Put `django.forms` in installed apps
- Scratch your head about where to put this snippet
{{{
import django

django.__path__[0] + "/forms/templates" # or '/forms/jinja2'
}}}
- Realize that this one was not necessary for the example, and profit, the
example is working

----

Now, with having `DjangoTemplates` instead of `TemplatesSetting`:
- You create a new django project, without any customization
- You create a form view
- You copy paste snippets from
https://docs.djangoproject.com/en/5.1/topics/forms/#reusable-form-
templates
- It's working, and you can start have customized form templates.

So maybe i missed a point and i hope you will tell me before closing the
ticket, but as an end user of the documentation it seems for me unecessary
complicated. With `DjangoTemplates` you go straight to the point, and you
can re-use form templates, just as the docs sections says
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:5>

Django

unread,
Jan 2, 2025, 4:00:28 AM1/2/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
------------------------------------+--------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Uncategorized | Status: new
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* cc: David Smith (added)

Comment:

https://docs.djangoproject.com/en/5.1/ref/forms/renderers/#django.forms.renderers.DjangoTemplates
> If you want to render templates with customizations from your TEMPLATES
setting, such as context processors for example, use the TemplatesSetting
renderer.

I think the above might be why customized renderers inherit from
`TemplatesSetting`.
Occasionally in examples we have a comment like `# ...` to indicate
something is incomplete/partial for the example. So perhaps a comment or
having it inherit from `DjangoTemplates` is an idea.

I'd quite like to hear David's opinion here
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:6>

Django

unread,
Jan 2, 2025, 4:05:57 AM1/2/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* type: Uncategorized => Cleanup/optimization

--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:7>

Django

unread,
Jan 2, 2025, 9:23:02 AM1/2/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
--------------------------------------+------------------------------------
Reporter: Guillaume LEBRETON | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Tentatively accepting
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:8>

Django

unread,
Jan 3, 2025, 4:37:26 PM1/3/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: Guillaume
Type: | LEBRETON
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Guillaume LEBRETON):

* has_patch: 0 => 1
* owner: (none) => Guillaume LEBRETON
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:9>

Django

unread,
Jan 3, 2025, 4:41:32 PM1/3/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: Guillaume
Type: | LEBRETON
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Guillaume LEBRETON):

https://github.com/django/django/pull/18999
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:10>

Django

unread,
Jan 7, 2025, 5:49:16 AM1/7/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: Guillaume
Type: | LEBRETON
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:11>

Django

unread,
Jan 24, 2025, 9:02:57 AM1/24/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: Guillaume
Type: | LEBRETON
Cleanup/optimization | Status: closed
Component: Documentation | Version: 5.1
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => wontfix
* status: assigned => closed

Comment:

Following a discussion on the pull request, we have decided to leave the
docs as they are.
Thank you all for the discussion
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:12>

Django

unread,
Apr 7, 2025, 6:02:16 AM4/7/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: Guillaume
Type: | LEBRETON
Cleanup/optimization | Status: closed
Component: Documentation | Version: 5.1
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Kevin Renskers):

I ran into the exact same problem: when following the docs on
https://docs.djangoproject.com/en/5.1/topics/forms/#reusable-form-
templates with a standard Django project, created by the startproject
command, you get a TemplateDoesNotExist error.

And no, I didn't have TemplatesSetting configured, since the default
project settings don't do that. And this is not mentioned at
https://docs.djangoproject.com/en/5.1/topics/forms/#reusable-form-
templates at all. So I find it a bit strange that this issue got closed
without any changes to the docs at all. Can the documentation at least
tell users that they need to add `django.forms` to their `INSTALLED_APPS`
for this to work?

> I'm unsure if modifying the example would improve it since what works
"out of the box" for one project may not work for another.

The example on https://docs.djangoproject.com/en/5.1/topics/forms
/#reusable-form-templates results in an error ''by default'', with a
standard Django project as created by the startproject command. It's a
pretty bad user experience when you run into this and you're scratching
your head why the example doesn't work.
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:13>

Django

unread,
Apr 7, 2025, 1:16:26 PM4/7/25
to django-...@googlegroups.com
#36044: Reusable form templates example lead to a TemplateDoesNotExist error
-------------------------------------+-------------------------------------
Reporter: Guillaume LEBRETON | Owner: Guillaume
Type: | LEBRETON
Cleanup/optimization | Status: closed
Component: Documentation | Version: 5.1
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Ahmed Nassar):

Hi everyone,
Can I work on and address this ticket? Is it still relevant? I will be
happy to work and submit my PR.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/36044#comment:14>
Reply all
Reply to author
Forward
0 new messages