I am writing a Django widget in my app. However, as the widget is an extension of a <textarea>
I would like to include within it the Django Textarea widget: django/forms/templates/django/forms/widgets/textarea.html
.
I tried this:
<div name="{{ widget.name }}"{% include "django/forms/widgets/textfield.html" %}>
{% if widget.value %}{{ widget.value }}{% endif %}
</div>
However, it resulted in the following error:
TemplateDoesNotExist at /admin/myApp/myModel/add/
How should I refer the include
statement to the proper template file?
Navigate to Design » Widget Templates » Create a template. The Create template form appears.
To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.
The widget in Django will handle the rendering of the HTML elements and extract data from a POST/GET dictionary that corresponds to the same widget. Whenever we specify a field on a Django form, Django uses some default widget appropriate to the data type to be displayed.
A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <! DOCTYPE html> .
You should add this setting on your Django's setting file:
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
Refer to: https://docs.djangoproject.com/en/3.0/ref/forms/renderers/#templatessetting
So Django is able to find the built-in template file {% include "django/forms/widgets/attrs.html" %}
and you get rid of the TemplateDoesNotExist
error.
forms.py
from my_app.widgets import CustomTextArea
class MyForm(ModelForm):
message = forms.CharField(max_length=500,widget=CustomTextArea(attrs={'cols': 10,'rows': 3,'class':'form-control'}),required=True,help_text=_('The max length of the text is 500.'))
widgets.py
from django import forms
class CustomTextArea(forms.Textarea):
template_name = 'django/forms/widgets/textfield.html'
Please create textfield.html
inside in templates folder in my_app
with correct path if you required.I follow this location(my_app/templates/django/forms/widgets/textfield.html
).You should must follow this when use this code.
textfield.html
1st method:Only show correct widget not altering in html.
{% include "django/forms/widgets/textarea.html" %}
{% if widget.value %}{{ widget.value }}{% endif %}
is equalent to:
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
2nd method: we can renders <textarea>...</textarea>
as inside div
.then,
<div>
{% include "django/forms/widgets/textarea.html" %}
{% if widget.value %}{{ widget.value }}{% endif %}
</div>
is equalent to:
<div>
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With