Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django widget template override does not search in the project template directory. How to fix?

Tags:

I am attempting to override a built in widget template in Django 1.11. I seem to be doing everything that the docs say to do in this regard, but for the widget templates, Django is not looking in my project at all, and I get a TemplateDoesNotExist error.

Here's what I have for the override:

class MyFileWidget(widgets.FileInput):     template_name = 'myapp/my_file_widget.html' 

The template is definitely there. If I pass the template to a render call, it finds it fine. Problem is an issue of paths. When calling render from a view, it checks the following:

projectroot/templates/myapp/my_file_widget.html djangoroot/forms/templates/myapp/my_file_widget.html

When it finds the template in my project, it renders it. This is NOT happening when I provide the template path in the class above. In that case, it does not check in my project templates, where the file actually exists, and begins checking in the django path, where it does not. Hence the error message.

So I have no clue why this the loader would check my project templates on render calls, but then fail to do so when looking for the "template_name" of the widget override. Any ideas?

like image 452
meesterguyperson Avatar asked Oct 19 '17 17:10

meesterguyperson


People also ask

How do I override a Django template?

To override these templates, you will need to have an admin folder in your templates folder. If you do not have a templates folder, you can create it in the main project folder. To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly.

What does {{ NAME }} this mean in Django templates?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

Where is the template directory in Django?

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.


1 Answers

By default, the FORM_RENDERER setting defaults to 'django.forms.renderers.DjangoTemplates'.

This checks your apps' templates directory (e.g. projectroot/myapp/templates/myapp/my_file_widget.html), but it does not check your project's template directories (e.g. projectroot/templates/myapp/my_file_widget.html).

If you want to use the same configuration as your TEMPLATES setting, then you should use the TemplatesSetting renderer.

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' 

You will also need to update your TEMPLATES setting so that Django can still use the built in widget templates. The easiest way to do this is to add django.forms to INSTALLED_APPS. See the docs for more info about this.

like image 146
Alasdair Avatar answered Sep 19 '22 22:09

Alasdair