TemplateDoesNotExist with django form field

December 16, 2020


Django ignores your template settings by default when rendering forms.


The error it was giving was TemplateDoesNotExist but the template definitely did exist. Thankfully, the dev server shows which loaders it tries to use and the order. Scanning through that it wasn't using any of my apps! What?


So apparently if you're rendering a form with a custom widget by default django won't use the TEMPLATES configuration from the settings.py file.


The default form renderer https://docs.djangoproject.com/en/3.1/ref/forms/renderers/#the-low-level-render-api needs changed to the TemplateSettings renderer...


So just add this to your settings:


FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'


and add django.forms to your installed apps as described here https://docs.djangoproject.com/en/3.1/ref/forms/renderers/#django.forms.renderers.TemplatesSetting.



And then it'll find the custom form field widget template...


Hope it helps, have a good one!