#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.