[Django]-Django. TemplateDoesNotExist in case of a custom widget

11👍

Its certainly not a bug

I don’t understand where does this django/forms/templates path come from?

You can view the source code where you can see the line

[docs]class Textarea(Widget):
    template_name = 'django/forms/widgets/textarea.html'

This was the source of your first question. Now second one

This renderer uses a standalone DjangoTemplates engine (unconnected to what you might have configured in the TEMPLATES setting). It loads templates first from the built-in form templates directory in django/forms/templates and then from the installed apps’ templates directories using the app_directories loader.

This is true for your form widget classes also. To make things work for you custom widget template you have to specify the path with same terminology like app_name/forms/widget/textarea.html

55👍

If you want to use a custom widget template stored somewhere under your “TEMPLATES” directory of your project then follow these steps:

a) Use the TEMPLATES settings that you have provided in your question

b) Set the FORM_RENDERER as following in the settings.py

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

c) Add the app “django.forms” to the ‘INSTALLED_APPS‘ list in settings.py

Also, be sure to assign the correct path of the custom widget template relative to your “TEMPLATES” directory to “template_name” attribute of your custom widget.

Leave a comment