Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a built-in django widget template in my custom widget template?

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?

like image 671
aviya.developer Avatar asked Feb 06 '20 12:02

aviya.developer


People also ask

How do I add a widget to my template?

Navigate to Design » Widget Templates » Create a template. The Create template form appears.

How do I add a template to Django project?

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.

What is the significance of introducing custom widgets in Django?

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.

What a widget is Django how can you use it HTML input elements?

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


2 Answers

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.

like image 71
Jônatas Castro Avatar answered Oct 12 '22 23:10

Jônatas Castro


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>
like image 29
Riyas Ac Avatar answered Oct 12 '22 22:10

Riyas Ac